Skip to content

Commit

Permalink
[Fix] Watched queries retriggers (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney authored Mar 25, 2024
1 parent bd71bb1 commit 8fc2164
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-vans-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@journeyapps/powersync-sdk-common': patch
---

Fixed regression where watched queries would update for table changes in external (not in query) tables.
5 changes: 5 additions & 0 deletions .changeset/shaggy-apples-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@journeyapps/powersync-sdk-web': patch
---

Minor code cleanup for shared sync worker.
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
const flushTableUpdates = throttle(
() => {
if (changedTables.size > 0) {
eventOptions.push({
changedTables: [...changedTables]
});
const intersection = Array.from(changedTables.values()).filter((change) => watchedTables.has(change));
if (intersection.length) {
eventOptions.push({
changedTables: intersection
});
}
}
changedTables.clear();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
};
}

uploadCrud(): Promise<void> {
return this.options.uploadCrud();
async uploadCrud(): Promise<void> {
/**
* Don't return anything here, just incase something which is not
* serializable is returned from the `uploadCrud` function.
*/
await this.options.uploadCrud();
}

get logger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class SharedSyncImplementation

async disconnect() {
this.abortController?.abort('Disconnected');
this.iterateListeners((l) => l.statusChanged?.(new SyncStatus({ connected: false })));
this.updateAllStatuses({ connected: false });
}

/**
Expand Down
42 changes: 42 additions & 0 deletions packages/powersync-sdk-web/tests/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,46 @@ describe('Watch Tests', () => {
// There should be one initial result plus one throttled result
expect(receivedUpdatesCount).equals(2);
});

it('should only watch tables inside query', async () => {
const assetsAbortController = new AbortController();

const watchAssets = powersync.watch('SELECT count() AS count FROM assets', [], {
signal: assetsAbortController.signal
});

const customersAbortController = new AbortController();

const watchCustomers = powersync.watch('SELECT count() AS count FROM customers', [], {
signal: customersAbortController.signal
});

let receivedAssetsUpdatesCount = 0;
// Listen to assets updates
(async () => {
for await (const update of watchAssets) {
receivedAssetsUpdatesCount++;
}
})();

let receivedCustomersUpdatesCount = 0;
(async () => {
for await (const update of watchCustomers) {
receivedCustomersUpdatesCount++;
}
})();

// Create the inserts as fast as possible
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]);

await new Promise<void>((resolve) => setTimeout(resolve, throttleDuration * 2));
assetsAbortController.abort();
customersAbortController.abort();

// There should be one initial result plus one throttled result
expect(receivedAssetsUpdatesCount).equals(2);

// Only the initial result should have yielded.
expect(receivedCustomersUpdatesCount).equals(1);
});
});

0 comments on commit 8fc2164

Please sign in to comment.