Skip to content

(feat): Added optional lock context for diff trigger creation and disposal#888

Merged
Chriztiaan merged 3 commits intomainfrom
feat/trigger-lock-context
Mar 12, 2026
Merged

(feat): Added optional lock context for diff trigger creation and disposal#888
Chriztiaan merged 3 commits intomainfrom
feat/trigger-lock-context

Conversation

@Chriztiaan
Copy link
Contributor

@Chriztiaan Chriztiaan commented Mar 11, 2026

For the query-driven sync implementation of powersync-ja/temp-tanstack-db#6, we need to recreate the diff trigger as the live queries clause set changes. Recreation involves disposing the current trigger and calling create again with the new new clause set. During testing of this feature we found edge cases where if mutations against the TanstackDB collection were still pending when we called dispose+create the mutations would be dropped.

The simplest approach to fixing this is to flush the data and then dispose and recreate the trigger- all during a single write lock. Diff triggers currently do not work inside a write lock and don't accept a lock option.
To support this work, this PR updates the diff trigger and cleanup callback to take in an optional lock context which is used instead of the db reference when provided.

For the TanstackDB PR it changed the usage from:

await flushDiffRecords()
await disposeTracking?.()

disposeTracking = await createDiffTrigger({
    ...

to

await database.writeLock(async (ctx) => {
    await flushDiffRecordsWithContext(ctx)
    await disposeTracking?.({ context: ctx })

    disposeTracking = await createDiffTrigger({
        setupContext: ctx,
        ...

Alternative

An alternative solution which worked to an extent was #881, which put full control of the diff trigger's life cycle in the user's hands. While useful, it does complicate the API and would need some more work to be able to work in a multiple tab scenario.

…ntext to the cleanup function returned by createDiffTrigger, this allows you to dispose and recreate a diffTrigger inside a single writeLock.
@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2026

🦋 Changeset detected

Latest commit: fcc7bf1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@powersync/common Minor
@powersync/adapter-sql-js Patch
@powersync/node Patch
@powersync/op-sqlite Patch
@powersync/react-native Patch
@powersync/tanstack-react-query Patch
@powersync/web Patch
@powersync/diagnostics-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Chriztiaan Chriztiaan marked this pull request as ready for review March 11, 2026 12:41
Copy link
Collaborator

@stevensJourney stevensJourney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here make sense and look good to me.

@Chriztiaan Chriztiaan merged commit 8ff6b3e into main Mar 12, 2026
9 checks passed
@Chriztiaan Chriztiaan deleted the feat/trigger-lock-context branch March 12, 2026 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants