Skip to content

Conversation

stevensJourney
Copy link
Contributor

@stevensJourney stevensJourney commented Mar 3, 2025

Overview

This PR improves the watched query logic by optimizing table update handling, enhancing thread safety, and simplifying the implementation.

Table Update Optimization

  • Table changes in a transaction are now tracked using a Set, preventing redundant entries. Since SQLite emits an update per row, the previous implementation could accumulate large arrays of duplicate table changes.

Thread Safety for Table Updates

  • Resolves issue #135, where the current driver’s pending update storage was not thread-safe.
  • The pending table changes set is now protected with Mutex locks.

Implementation Cleanup

  • Replaces the existing SQLDelight watched query interface with a simpler, Flow-based solution.
  • The new approach leverages the internal database’s getAll method instead of the driver’s synchronous methods.
  • Table change flow logic has been refactored for better debouncing, allowing custom throttleMs values per query.

Removal of Blocking Calls

  • Previously, the watch query API synchronously determined the tables to watch, which impacted performance (e.g., in the Supabase Todo List app). This logic now uses the asynchronous getAll method, improving efficiency.

Testing

This was tested locally using both the Kotlin and Swift SDKs.

watches.mp4

Swift

Related Swift PR powersync-ja/powersync-swift#26

@stevensJourney stevensJourney marked this pull request as ready for review March 5, 2025 08:50
@stevensJourney stevensJourney requested a review from simolus3 March 5, 2025 08:50
Copy link
Contributor

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

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

Thanks! I'm happy with the refactoring, and most of these comments are nits.

Copy link
Contributor

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

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

LGTM!

@stevensJourney stevensJourney merged commit 025b1d2 into main Mar 5, 2025
3 checks passed
@stevensJourney stevensJourney deleted the watches branch March 5, 2025 11:38
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.

Race condition in PsSqlDriver?

2 participants