Skip to content
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/fresh-fishes-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@journeyapps/powersync-sdk-common': patch
---

Resolving tables for watch() before handling any results, eliminating a potential race condition between initial result and changes. Also handling a potential uncaught exception.
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
}

/**
* Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set
* and optionally return results.
* Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set
* and optionally return results.
* This is faster than executing separately with each parameter set.
*/
async executeBatch(sql: string, parameters?: any[][]) {
Expand Down Expand Up @@ -637,13 +637,24 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB

(async () => {
try {
// Fetch initial data
onResult(await this.executeReadOnly(sql, parameters));

const resolvedTables = await this.resolveTables(sql, parameters, options);

// Fetch initial data
const result = await this.executeReadOnly(sql, parameters);
onResult(result);

this.onChangeWithCallback(
{ onChange: async () => onResult(await this.executeReadOnly(sql, parameters)), onError },
{
onChange: async () => {
try {
const result = await this.executeReadOnly(sql, parameters);
onResult(result);
} catch (error) {
onError?.(error);
}
},
onError
},
{
...(options ?? {}),
tables: resolvedTables
Expand All @@ -663,11 +674,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
watchWithAsyncGenerator(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult> {
return new EventIterator<QueryResult>((eventOptions) => {
(async () => {
const resolvedTables = await this.resolveTables(sql, parameters, options);

// Fetch initial data
eventOptions.push(await this.executeReadOnly(sql, parameters));

const resolvedTables = await this.resolveTables(sql, parameters, options);

for await (const event of this.onChangeWithAsyncGenerator({
...(options ?? {}),
tables: resolvedTables
Expand Down
7 changes: 6 additions & 1 deletion packages/powersync-sdk-web/tests/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ describe('Watch Tests', () => {
}
})();

// Ensures insert doesn't form part of initial result
await new Promise<void>((resolve) => setTimeout(resolve, throttleDuration));

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

Expand Down Expand Up @@ -238,7 +241,9 @@ describe('Watch Tests', () => {
}
);

// Create the inserts as fast as possible
// Ensures insert doesn't form part of initial result
await new Promise<void>((resolve) => setTimeout(resolve, throttleDuration));

await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]);

await new Promise<void>((resolve) => setTimeout(resolve, throttleDuration * 2));
Expand Down