Skip to content

Commit 8fc2164

Browse files
[Fix] Watched queries retriggers (#102)
1 parent bd71bb1 commit 8fc2164

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

.changeset/metal-vans-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@journeyapps/powersync-sdk-common': patch
3+
---
4+
5+
Fixed regression where watched queries would update for table changes in external (not in query) tables.

.changeset/shaggy-apples-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@journeyapps/powersync-sdk-web': patch
3+
---
4+
5+
Minor code cleanup for shared sync worker.

packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
592592
const flushTableUpdates = throttle(
593593
() => {
594594
if (changedTables.size > 0) {
595-
eventOptions.push({
596-
changedTables: [...changedTables]
597-
});
595+
const intersection = Array.from(changedTables.values()).filter((change) => watchedTables.has(change));
596+
if (intersection.length) {
597+
eventOptions.push({
598+
changedTables: intersection
599+
});
600+
}
598601
}
599602
changedTables.clear();
600603
},

packages/powersync-sdk-web/src/db/sync/SharedWebStreamingSyncImplementation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
4242
};
4343
}
4444

45-
uploadCrud(): Promise<void> {
46-
return this.options.uploadCrud();
45+
async uploadCrud(): Promise<void> {
46+
/**
47+
* Don't return anything here, just incase something which is not
48+
* serializable is returned from the `uploadCrud` function.
49+
*/
50+
await this.options.uploadCrud();
4751
}
4852

4953
get logger() {

packages/powersync-sdk-web/src/worker/sync/SharedSyncImplementation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export class SharedSyncImplementation
205205

206206
async disconnect() {
207207
this.abortController?.abort('Disconnected');
208-
this.iterateListeners((l) => l.statusChanged?.(new SyncStatus({ connected: false })));
208+
this.updateAllStatuses({ connected: false });
209209
}
210210

211211
/**

packages/powersync-sdk-web/tests/watch.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,46 @@ describe('Watch Tests', () => {
101101
// There should be one initial result plus one throttled result
102102
expect(receivedUpdatesCount).equals(2);
103103
});
104+
105+
it('should only watch tables inside query', async () => {
106+
const assetsAbortController = new AbortController();
107+
108+
const watchAssets = powersync.watch('SELECT count() AS count FROM assets', [], {
109+
signal: assetsAbortController.signal
110+
});
111+
112+
const customersAbortController = new AbortController();
113+
114+
const watchCustomers = powersync.watch('SELECT count() AS count FROM customers', [], {
115+
signal: customersAbortController.signal
116+
});
117+
118+
let receivedAssetsUpdatesCount = 0;
119+
// Listen to assets updates
120+
(async () => {
121+
for await (const update of watchAssets) {
122+
receivedAssetsUpdatesCount++;
123+
}
124+
})();
125+
126+
let receivedCustomersUpdatesCount = 0;
127+
(async () => {
128+
for await (const update of watchCustomers) {
129+
receivedCustomersUpdatesCount++;
130+
}
131+
})();
132+
133+
// Create the inserts as fast as possible
134+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]);
135+
136+
await new Promise<void>((resolve) => setTimeout(resolve, throttleDuration * 2));
137+
assetsAbortController.abort();
138+
customersAbortController.abort();
139+
140+
// There should be one initial result plus one throttled result
141+
expect(receivedAssetsUpdatesCount).equals(2);
142+
143+
// Only the initial result should have yielded.
144+
expect(receivedCustomersUpdatesCount).equals(1);
145+
});
104146
});

0 commit comments

Comments
 (0)