git » sdk » commit b9b0263

Throttle chats updated events

author Stephen Paul Weber
2025-10-03 02:43:56 UTC
committer Stephen Paul Weber
2025-10-03 02:43:56 UTC
parent 77bf8641519432187f0848959d0069c0aef868a9

Throttle chats updated events

Since presence floods can make them frequent and updating UI too often
is just a CPU hog

borogove/Client.hx +14 -2

diff --git a/borogove/Client.hx b/borogove/Client.hx
index 7e0be40..c22d6d8 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -1213,9 +1213,21 @@ class Client extends EventEmitter {
 
 		@param handler takes one argument, an array of Chats that were updated
 	**/
+	private final updateChatBuffer: Map<String, Chat> = [];
+	private var updateChatTimer = null;
 	public function addChatsUpdatedListener(handler:Array<Chat>->Void):Void {
-		this.on("chats/update", (data) -> {
-			handler(data);
+		this.on("chats/update", (data: Array<Chat>) -> {
+			if (updateChatTimer != null) {
+				updateChatTimer.stop();
+			}
+			for (chat in data) {
+				updateChatBuffer[chat.chatId] = chat;
+			}
+			updateChatTimer = haxe.Timer.delay(() -> {
+				handler({ iterator: updateChatBuffer.iterator }.array());
+				updateChatTimer = null;
+				updateChatBuffer.clear();
+			}, 500);
 			return EventHandled;
 		});
 	}