git » sdk » commit 78091ac

Wait for all storage operations to be done

author Stephen Paul Weber
2024-07-22 19:31:03 UTC
committer Stephen Paul Weber
2024-07-22 19:31:03 UTC
parent 468720c90c4b126ddb2032c0929542a581eb38c7

Wait for all storage operations to be done

BEFORE we call the callback and indicate that sync is complete

snikket/Client.hx +8 -3

diff --git a/snikket/Client.hx b/snikket/Client.hx
index acc2401..dcba8e9 100644
--- a/snikket/Client.hx
+++ b/snikket/Client.hx
@@ -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
 				}
@@ -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) -> {