git » sdk » commit 5a95932

Wait for writes after each page from MAM

author Stephen Paul Weber
2024-11-07 18:16:16 UTC
committer Stephen Paul Weber
2024-11-07 18:16:16 UTC
parent 70dcf760d249c5e786a91143c9b57506d1916726

Wait for writes after each page from MAM

Instead of queuing them all up until the end and then after MAM is
"done" on a slow system we may still have thousands of writes left to
wait for.

snikket/Chat.hx +7 -7
snikket/Client.hx +15 -17

diff --git a/snikket/Chat.hx b/snikket/Chat.hx
index cb1e46b..68ba702 100644
--- a/snikket/Chat.hx
+++ b/snikket/Chat.hx
@@ -943,9 +943,9 @@ class Channel extends Chat {
 			chatId
 		);
 		sync.setNewestPageFirst(false);
-		final promises = [];
 		final chatMessages = [];
 		sync.onMessages((messageList) -> {
+			final promises = [];
 			for (m in messageList.messages) {
 				switch (m) {
 					case ChatMessageStanza(message):
@@ -964,10 +964,10 @@ class Channel extends Chat {
 						// ignore
 				}
 			}
-			if (sync.hasMore()) {
-				sync.fetchNext();
-			} else {
-				thenshim.PromiseTools.all(promises).then((_) -> {
+			thenshim.PromiseTools.all(promises).then((_) -> {
+				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)) {
@@ -981,8 +981,8 @@ class Channel extends Chat {
 						setUnreadCount(chatMessages.length - readIndex - 1);
 					}
 					client.trigger("chats/update", [this]);
-				});
-			}
+				}
+			});
 		});
 		sync.onError((stanza) -> {
 			if (lastId != null) {
diff --git a/snikket/Client.hx b/snikket/Client.hx
index 8eaf571..e26b047 100644
--- a/snikket/Client.hx
+++ b/snikket/Client.hx
@@ -1347,8 +1347,8 @@ class Client extends EventEmitter {
 			lastId == null ? { startTime: thirtyDaysAgo } : { page: { after: lastId } }
 		);
 		sync.setNewestPageFirst(false);
-		final promises = [];
 		sync.onMessages((messageList) -> {
+			final promises = [];
 			for (m in messageList.messages) {
 				switch (m) {
 					case ChatMessageStanza(message):
@@ -1363,23 +1363,21 @@ class Client extends EventEmitter {
 						// ignore
 				}
 			}
-			if (sync.hasMore()) {
-				sync.fetchNext();
-			} else {
-				for (sid => stanza in sync.jmi) {
-					onMAMJMI(sid, stanza);
-				}
-				if (callback != null) {
-					thenshim.PromiseTools.all(promises)
-						.then(
-							(_) -> callback(true),
-							(e) -> {
-								trace("SYNC: error", e);
-								callback(false);
-							}
-						);
+			trace("SYNC: MAM page wait for writes");
+			thenshim.PromiseTools.all(promises).then((_) -> {
+				if (sync.hasMore()) {
+					sync.fetchNext();
+				} else {
+					for (sid => stanza in sync.jmi) {
+						onMAMJMI(sid, stanza);
+					}
+					if (callback != null) callback(true);
 				}
-			}
+			},
+			(e) -> {
+				trace("SYNC: error", e);
+				callback(false);
+			});
 		});
 		sync.onError((stanza) -> {
 			if (lastId != null) {