From 360a44c3870a4c99cdb469e444636ab7d7bc46a4 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 12 Aug 2024 13:32:46 -0500 Subject: [PATCH] Wait until sync is done to mark inSync = true Including all persistence complete. --- snikket/Chat.hx | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/snikket/Chat.hx b/snikket/Chat.hx index ec76b7b..3477eb7 100644 --- a/snikket/Chat.hx +++ b/snikket/Chat.hx @@ -731,15 +731,20 @@ class Channel extends Chat { chatId ); sync.setNewestPageFirst(false); + final promises = []; final chatMessages = []; sync.onMessages((messageList) -> { for (m in messageList.messages) { switch (m) { case ChatMessageStanza(message): - persistence.storeMessage(client.accountId(), message, (m)->{}); + promises.push(new thenshim.Promise((resolve, reject) -> { + persistence.storeMessage(client.accountId(), message, resolve); + })); if (message.chatId() == chatId) chatMessages.push(message); case ReactionUpdateStanza(update): - persistence.storeReaction(client.accountId(), update, (m)->{}); + promises.push(new thenshim.Promise((resolve, reject) -> { + persistence.storeReaction(client.accountId(), update, resolve); + })); default: // ignore } @@ -747,13 +752,21 @@ class Channel extends Chat { if (sync.hasMore()) { sync.fetchNext(); } else { - inSync = true; - final lastFromSync = chatMessages[chatMessages.length - 1]; - if (lastFromSync != null && (lastMessageTimestamp() == null || Reflect.compare(lastFromSync.timestamp, lastMessageTimestamp()) > 0)) { - setLastMessage(lastFromSync); - client.sortChats(); - } - client.trigger("chats/update", [this]); + thenshim.PromiseTools.all(promises).then((_) -> { + inSync = true; + final lastFromSync = chatMessages[chatMessages.length - 1]; + if (lastFromSync != null && (lastMessageTimestamp() == null || Reflect.compare(lastFromSync.timestamp, lastMessageTimestamp()) > 0)) { + setLastMessage(lastFromSync); + client.sortChats(); + } + final readIndex = chatMessages.findIndex((m) -> m.serverId == readUpTo()); + if (readIndex < 0) { + setUnreadCount(unreadCount() + chatMessages.length); + } else { + setUnreadCount(chatMessages.length - readIndex - 1); + } + client.trigger("chats/update", [this]); + }); } }); sync.onError((stanza) -> {