Skip to content

Commit

Permalink
Illustrating fix for broadcasting loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriztiaan committed Jan 27, 2025
1 parent 428686a commit 4a52239
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ export class WASqliteConnection
// Ignore messages from the same connection
return;
}
this.queueTableUpdate(data.changedTables);
this.queueTableUpdate(data.changedTables, true);
});
}

protected queueTableUpdate(tableNames: Set<string>) {
protected queueTableUpdate(tableNames: Set<string>, fromBroadcast = false) {
tableNames.forEach((tableName) => this.updatedTables.add(tableName));
if (this.updateTimer == null) {
this.updateTimer = setTimeout(() => this.fireUpdates(), 0);
this.updateTimer = setTimeout(() => this.fireUpdates(fromBroadcast), 0);
}
}

Expand All @@ -248,14 +248,16 @@ export class WASqliteConnection
return this.options;
}

fireUpdates() {
fireUpdates(fromBroadcast = false) {
this.updateTimer = null;
const event: BatchedUpdateNotification = { tables: [...this.updatedTables], groupedUpdates: {}, rawUpdates: [] };
// Share to other connections
this.broadcastChannel!.postMessage({
changedTables: this.updatedTables,
connectionId: this.connectionId
} satisfies WASQLiteBroadCastTableUpdateEvent);
if (!fromBroadcast) {
this.broadcastChannel!.postMessage({
changedTables: this.updatedTables,
connectionId: this.connectionId
} satisfies WASQLiteBroadCastTableUpdateEvent);
}
this.updatedTables.clear();
this.iterateListeners((cb) => cb.tablesUpdated?.(event));
}
Expand Down

0 comments on commit 4a52239

Please sign in to comment.