Skip to content

Compile :common project for the web#354

Draft
simolus3 wants to merge 3 commits into
mainfrom
common-web
Draft

Compile :common project for the web#354
simolus3 wants to merge 3 commits into
mainfrom
common-web

Conversation

@simolus3

Copy link
Copy Markdown
Contributor

This is blocked on androidx.sqlite version 2.7.0, which is currently in alpha.

This adds support for compiling (most) of the :common project for the web. This doesn't actually add web support, but it gets :common into a state where providing a web implementation of our SQLiteConnectionPool interface would make it run on the web. A future PR would provide that as a builtin option.

The key challenge here is that we assume synchronous SQLite calls in many places of the SDK. On the web however, preparing or stepping through statements is fundamentally asynchronous. To reconcile this without a breaking change, we adopt this pattern:

  1. On non-web platforms, both blocking and non-blocking APIs are available (previously, we only had blocking APIs). Non-blocking APIs are implemented by just calling the blocking APIs in a suspend function, which is fine because we run the entire callback in a Dispatchers.IO context.
  2. On the web, only the non-blocking APIs are available.

In Kotlin, we can express this with expect interfaces:

  • In common code, we declare expect interfaces with only the non-blocking APIs.
  • In non-web targets, we use an actual interface also adding the blocking APIs. This allows us to preserve both source and binary compatibility, since blocking methods still exist in the exact same location.

To be able to use suspending functions, this adds asynchronous methods to ConnectionContext and Queries. This also refactors the implementation of Queries to use default methods based on useConnection.

On the web, we initially won't support:

  • RSocket (since browsers are supposed to offer backpressure for fetch() streams).
  • The attachments system (it uses some callbacks that currently only work with blocking APIs, so porting it to the web might require breaking changes).
  • The PowerSyncDatabase() factory method (only PowerSyncDatabase.opened is supported).

There's obviously a lot more to do, but this should be a decent initial step.

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