Skip to content

Use spawned async tasks for sync - #40

Open
simolus3 wants to merge 7 commits into
mainfrom
async-refactor
Open

simolus3 wants to merge 7 commits into
mainfrom
async-refactor

Conversation

@simolus3

@simolus3 simolus3 commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

Spawning a background task (like the upload and download loops we spawn for connections) requires an asynchronous runtime, and the native SDK should support different option.

Currently, this is implemented by forcing the user to call async_tasks().spawn_with() to spawn asynchronous tasks before connecting. This was intended for compatibility with embedded frameworks where spawning tasks dynamically is not possible.

However, that greatly complicates the design of the sync client, as we need to start tasks that do nothing until connected, and need to handle reconnects as part of their state machine. This PR refactors the async setup to closer match what we have on our other SDKs: connect() spawns the tasks, disconnect() waits for them to complete. This allows turning e.g. the download actor (250+ lines before) into a trivial loop (around 15 lines). This also makes it easier to spawn additional sync tasks (e.g. for the checkpoint request loop) in the future..

This is a breaking change: Users need to construct their PowerSyncEnvironment differently to provide a mechanism to spawn tasks there.

AI use: Reviewed with Claude code.

@simolus3
simolus3 marked this pull request as ready for review September 18, 2026 14:05
@simolus3
simolus3 added this pull request to stack #42 September 18, 2026 15:48
@simolus3
simolus3 requested a review from Chriztiaan September 18, 2026 16:15
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.

1 participant