From 7cc64497a5afa1563dbb2dd4cc094fdedfb5f22f Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 29 Nov 2019 11:12:04 +0000 Subject: [PATCH] [Timeline] Add get event by id method --- lib/src/Room.dart | 7 +------ lib/src/Timeline.dart | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 95ca366..3ae71b8 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -758,13 +758,8 @@ class Room { return user; } - /// Searches for the event in the store. If it isn't found, try to request it - /// from the server. Returns null if not found. + /// Searches for the event on the server. Returns null if not found. Future getEventById(String eventID) async { - if (client.store != null) { - final Event storeEvent = await client.store.getEventById(eventID, this); - if (storeEvent != null) return storeEvent; - } final dynamic resp = await client.connection.jsonRequest( type: HTTPType.GET, action: "/client/r0/rooms/$id/event/$eventID"); if (resp is ErrorResponse) return null; diff --git a/lib/src/Timeline.dart b/lib/src/Timeline.dart index ab0e16c..cf11323 100644 --- a/lib/src/Timeline.dart +++ b/lib/src/Timeline.dart @@ -44,6 +44,22 @@ class Timeline { StreamSubscription sub; bool _requestingHistoryLock = false; + Map _eventCache = {}; + + /// Searches for the event in this timeline. If not + /// found, requests from the server. Requested events + /// are cached. + Future getEventById(String id) async { + for (int i = 0; i < events.length; i++) { + if (events[i].eventId == id) return events[i]; + } + if (_eventCache.containsKey(id)) return _eventCache[id]; + final Event requestedEvent = await room.getEventById(id); + if (requestedEvent == null) return null; + _eventCache[id] = requestedEvent; + return _eventCache[id]; + } + Future requestHistory( {int historyCount = Room.DefaultHistoryCount}) async { if (!_requestingHistoryLock) {