Skip to content

Commit

Permalink
Wait until sync is done to mark inSync = true
Browse files Browse the repository at this point in the history
Including all persistence complete.
  • Loading branch information
singpolyma committed Aug 12, 2024
1 parent b0f5e85 commit 360a44c
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions snikket/Chat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -731,29 +731,42 @@ 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
}
}
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) -> {
Expand Down

0 comments on commit 360a44c

Please sign in to comment.