git » sdk » commit 0748232

Update chat right away, then debounce

author Stephen Paul Weber
2025-11-11 15:34:36 UTC
committer Stephen Paul Weber
2025-11-11 15:54:32 UTC
parent 45a54a2711ebcf324365bd5b92670b17579639bd

Update chat right away, then debounce

Instead of delaying even the first update

borogove/Client.hx +12 -2

diff --git a/borogove/Client.hx b/borogove/Client.hx
index e7fd02c..1f20cda 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -1259,19 +1259,29 @@ class Client extends EventEmitter {
 	**/
 	public function addChatsUpdatedListener(handler:Array<Chat>->Void) {
 		final updateChatBuffer: Map<String, Chat> = [];
+		var lastCall = -1.0;
 		var updateChatTimer = null;
 		return this.on("chats/update", (data: Array<Chat>) -> {
+			final now = haxe.Timer.stamp() * 1000;
 			if (updateChatTimer != null) {
 				updateChatTimer.stop();
 			}
 			for (chat in data) {
 				updateChatBuffer[chat.chatId] = chat;
 			}
-			updateChatTimer = haxe.Timer.delay(() -> {
+			if (lastCall < 0 || now - lastCall >= 500) {
+				lastCall = now;
 				handler({ iterator: updateChatBuffer.iterator }.array());
 				updateChatTimer = null;
 				updateChatBuffer.clear();
-			}, 500);
+			} else {
+				updateChatTimer = haxe.Timer.delay(() -> {
+					lastCall = haxe.Timer.stamp() * 1000;
+					handler({ iterator: updateChatBuffer.iterator }.array());
+					updateChatTimer = null;
+					updateChatBuffer.clear();
+				}, 500);
+			}
 			return EventHandled;
 		});
 	}