Skip to content

[Fix] Watched queries retriggers #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -204,7 +204,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);
});
});