diff --git a/.changeset/warm-foxes-act.md b/.changeset/warm-foxes-act.md new file mode 100644 index 00000000..dc516f0e --- /dev/null +++ b/.changeset/warm-foxes-act.md @@ -0,0 +1,5 @@ +--- +'@journeyapps/powersync-sdk-common': patch +--- + +Fixed table change updates to be throttled and flushed on the trailing edge to avoid race conditions in watched queries. diff --git a/apps/supabase-todolist b/apps/supabase-todolist index ef67b639..3bb0b34b 160000 --- a/apps/supabase-todolist +++ b/apps/supabase-todolist @@ -1 +1 @@ -Subproject commit ef67b6397cf1cbf70c7512dfeb3eddb0719bc8c8 +Subproject commit 3bb0b34be007be5f5bd056b9585dad763559f3b1 diff --git a/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts b/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts index 3257e9b9..0a635033 100644 --- a/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts +++ b/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts @@ -499,15 +499,19 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver((eventOptions) => { - const flushTableUpdates = _.throttle(async () => { - const intersection = _.intersection(watchedTables, throttledTableUpdates); - if (intersection.length) { - eventOptions.push({ - changedTables: intersection - }); - } - throttledTableUpdates = []; - }, throttleMs); + const flushTableUpdates = _.throttle( + async () => { + const intersection = _.intersection(watchedTables, throttledTableUpdates); + if (intersection.length) { + eventOptions.push({ + changedTables: intersection + }); + } + throttledTableUpdates = []; + }, + throttleMs, + { leading: false, trailing: true } + ); const dispose = this.database.registerListener({ tablesUpdated: async (update) => { diff --git a/yarn.lock b/yarn.lock index a63f460f..ef834527 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2220,7 +2220,7 @@ "@types/yargs" "^17.0.8" chalk "^4.0.0" -"@journeyapps/react-native-quick-sqlite@^1.0.0", "@journeyapps/react-native-quick-sqlite@^1.1.0": +"@journeyapps/react-native-quick-sqlite@^1.1.0": version "1.1.0" resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-1.1.0.tgz#cf4aa6694b7232d0f86e565fdba4e41ef15d80cc" integrity sha512-Pg6VA6ABC7N5FrNB5eqTgNsKdzzmDSp5aBtnQh1BlcZu7ISPZdCcKo+ZJtKyzTAWpc17LIttvJwxez6zBxUdOw==