Skip to content

Commit 4a52239

Browse files
committed
Illustrating fix for broadcasting loop.
1 parent 428686a commit 4a52239

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ export class WASqliteConnection
217217
// Ignore messages from the same connection
218218
return;
219219
}
220-
this.queueTableUpdate(data.changedTables);
220+
this.queueTableUpdate(data.changedTables, true);
221221
});
222222
}
223223

224-
protected queueTableUpdate(tableNames: Set<string>) {
224+
protected queueTableUpdate(tableNames: Set<string>, fromBroadcast = false) {
225225
tableNames.forEach((tableName) => this.updatedTables.add(tableName));
226226
if (this.updateTimer == null) {
227-
this.updateTimer = setTimeout(() => this.fireUpdates(), 0);
227+
this.updateTimer = setTimeout(() => this.fireUpdates(fromBroadcast), 0);
228228
}
229229
}
230230

@@ -248,14 +248,16 @@ export class WASqliteConnection
248248
return this.options;
249249
}
250250

251-
fireUpdates() {
251+
fireUpdates(fromBroadcast = false) {
252252
this.updateTimer = null;
253253
const event: BatchedUpdateNotification = { tables: [...this.updatedTables], groupedUpdates: {}, rawUpdates: [] };
254254
// Share to other connections
255-
this.broadcastChannel!.postMessage({
256-
changedTables: this.updatedTables,
257-
connectionId: this.connectionId
258-
} satisfies WASQLiteBroadCastTableUpdateEvent);
255+
if (!fromBroadcast) {
256+
this.broadcastChannel!.postMessage({
257+
changedTables: this.updatedTables,
258+
connectionId: this.connectionId
259+
} satisfies WASQLiteBroadCastTableUpdateEvent);
260+
}
259261
this.updatedTables.clear();
260262
this.iterateListeners((cb) => cb.tablesUpdated?.(event));
261263
}

0 commit comments

Comments
 (0)