git » sdk » commit 66a14fc

Clearing is a bit operation

author Stephen Paul Weber
2026-07-27 18:27:55 UTC
committer Stephen Paul Weber
2026-07-27 18:27:55 UTC
parent 609a35b93059eb800211470d72c3781dc3c7aa50

Clearing is a bit operation

So reduce write: don't update unless we need to
Reduce allocations: don't use one promise per row or a clone

borogove/persistence/IDB.js +16 -6

diff --git a/borogove/persistence/IDB.js b/borogove/persistence/IDB.js
index 47f4169..6277596 100644
--- a/borogove/persistence/IDB.js
+++ b/borogove/persistence/IDB.js
@@ -641,15 +641,25 @@ export default async (dbname, media, tokenize, stemmer) => {
 			const store = tx.objectStore("members");
 			const range = IDBKeyRange.bound(chatId ? [account, chatId] : [account], chatId ? [account, chatId, []] : [account, []]);
 			const cursor = store.index("chatsWithTrueJid").openCursor(range);
-			while (true) {
-				const cresult = await promisifyRequest(cursor);
-				if (!cresult?.value) break;
+			await new Promise((resolve, reject) => {
+				cursor.onerror = () => reject(cursor.error);
+				cursor.onsuccess = (e) => {
+					const cresult = e.target.result;
+					if (!cresult?.value) {
+						resolve();
+						return;
+					}
 
-				cresult.update({ ...cresult.value, presence: new Map() });
+					if (cresult.value.presence?.size) {
+						cresult.value.presence = new Map();
+						cresult.update(cresult.value);
+					}
 
-				cresult.continue();
-			}
+					cresult.continue();
+				};
+			});
 
+			await promisifyRequest(tx);
 			return true;
 		},