Skip to content

Commit

Permalink
flush table change updates on trailing edge. Prevents race conditions…
Browse files Browse the repository at this point in the history
… in watched queries
  • Loading branch information
stevensJourney committed Jan 26, 2024
1 parent 7d4c6fe commit 63887f4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-foxes-act.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,19 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
const throttleMs = options.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS;

return new EventIterator<WatchOnChangeEvent>((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) => {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 63887f4

Please sign in to comment.