Skip to content

Commit

Permalink
Wait for all storage operations to be done
Browse files Browse the repository at this point in the history
BEFORE we call the callback and indicate that sync is complete
  • Loading branch information
singpolyma committed Jul 22, 2024
1 parent 468720c commit 78091ac
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions snikket/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1168,13 +1168,18 @@ class Client extends EventEmitter {
lastId == null ? { startTime: thirtyDaysAgo } : { page: { after: lastId } }
);
sync.setNewestPageFirst(false);
final promises = [];
sync.onMessages((messageList) -> {
for (m in messageList.messages) {
switch (m) {
case ChatMessageStanza(message):
persistence.storeMessage(accountId(), message, (m)->{});
promises.push(new thenshim.Promise((resolve, reject) -> {
persistence.storeMessage(accountId(), message, resolve);
}));
case ReactionUpdateStanza(update):
persistence.storeReaction(accountId(), update, (m)->{});
promises.push(new thenshim.Promise((resolve, reject) -> {
persistence.storeReaction(accountId(), update, (m)->{});
}));
default:
// ignore
}
Expand All @@ -1185,7 +1190,7 @@ class Client extends EventEmitter {
for (sid => stanza in sync.jmi) {
onMAMJMI(sid, stanza);
}
if (callback != null) callback();
if (callback != null) thenshim.PromiseTools.all(promises).then((_) -> callback());
}
});
sync.onError((stanza) -> {
Expand Down

0 comments on commit 78091ac

Please sign in to comment.