diff --git a/lib/models/message.dart b/lib/models/message.dart index da2a9411..2f5507c3 100644 --- a/lib/models/message.dart +++ b/lib/models/message.dart @@ -77,17 +77,23 @@ class ByIndex implements CacheHandler { } Future get( Cwtch cwtch, String profileOnion, int conversationIdentifier, MessageCache cache) async { - // observationally flutter future builder seemed to be reaching for 20-40 message on pane load, so we start trying to load up to that many messages in one request - var chunk = 40; - // check that we aren't asking for messages beyond stored messages - if (chunk > cache.storageMessageCount - index) { - chunk = cache.storageMessageCount - index; - } - + // if in cache, get if (index < cache.cacheByIndex.length) { return cache.getByIndex(index); } - cache.lockIndexs(index, index+chunk); + + // otherwise we are going to fetch, so we'll fetch a chunk of messages + // observationally flutter future builder seemed to be reaching for 20-40 message on pane load, so we start trying to load up to that many messages in one request + var chunk = 40; + // check that we aren't asking for messages beyond stored messages + if (index + chunk >= cache.storageMessageCount) { + chunk = cache.storageMessageCount - index; + if (chunk <= 0) { + return Future.value(null); + } + } + + cache.lockIndexes(index, index+chunk); var msgs = await cwtch.GetMessages(profileOnion, conversationIdentifier, index, chunk); int i = 0; // i used to loop through returned messages. if doesn't reach the requested count, we will use it in the finally stanza to error out the remaining asked for messages in the cache try { diff --git a/lib/models/messagecache.dart b/lib/models/messagecache.dart index 51c61be2..af418259 100644 --- a/lib/models/messagecache.dart +++ b/lib/models/messagecache.dart @@ -116,7 +116,7 @@ class MessageCache extends ChangeNotifier { // inserts place holder values into the index cache that will block on .get() until .finishLoad() is called on them with message contents // or .failLoad() is called on them to mark them malformed // this prevents successive ui message build requests from triggering multiple GetMesssage requests to the backend, as the first one locks a block of messages and the rest wait on that - void lockIndexs(int start, int end) { + void lockIndexes(int start, int end) { for(var i = start; i < end; i++) { this.cacheByIndex.insert(i, LocalIndexMessage(null, isLoading: true)); }