diff --git a/packages/powersync_core/lib/src/open_factory/web/web_open_factory.dart b/packages/powersync_core/lib/src/open_factory/web/web_open_factory.dart index bf4cf979..a6dfae51 100644 --- a/packages/powersync_core/lib/src/open_factory/web/web_open_factory.dart +++ b/packages/powersync_core/lib/src/open_factory/web/web_open_factory.dart @@ -3,15 +3,29 @@ import 'dart:async'; import 'package:powersync_core/src/open_factory/abstract_powersync_open_factory.dart'; import 'package:powersync_core/src/uuid.dart'; import 'package:sqlite_async/sqlite3_common.dart'; +import 'package:sqlite_async/sqlite3_web.dart'; import 'package:sqlite_async/sqlite_async.dart'; +import 'package:sqlite_async/web.dart'; + +import '../../web/worker_utils.dart'; /// Web implementation for [AbstractPowerSyncOpenFactory] -class PowerSyncOpenFactory extends AbstractPowerSyncOpenFactory { +class PowerSyncOpenFactory extends AbstractPowerSyncOpenFactory + implements WebSqliteOpenFactory { PowerSyncOpenFactory({ required super.path, super.sqliteOptions, }); + @override + Future openWebSqlite(WebSqliteOptions options) async { + return WebSqlite.open( + wasmModule: Uri.parse(sqliteOptions.webSqliteOptions.wasmUri), + worker: Uri.parse(sqliteOptions.webSqliteOptions.workerUri), + controller: PowerSyncAsyncSqliteController(), + ); + } + @override void enableExtension() { // No op for web diff --git a/packages/powersync_core/lib/src/web/powersync_db.worker.dart b/packages/powersync_core/lib/src/web/powersync_db.worker.dart index 8ce44f4b..4ae79365 100644 --- a/packages/powersync_core/lib/src/web/powersync_db.worker.dart +++ b/packages/powersync_core/lib/src/web/powersync_db.worker.dart @@ -4,30 +4,10 @@ library; -import 'dart:js_interop'; - -import 'package:sqlite_async/sqlite3_web_worker.dart'; import 'package:sqlite_async/sqlite3_web.dart'; -import 'package:sqlite_async/sqlite3_wasm.dart'; import 'worker_utils.dart'; void main() { WebSqlite.workerEntrypoint(controller: PowerSyncAsyncSqliteController()); } - -final class PowerSyncAsyncSqliteController extends AsyncSqliteController { - @override - Future openDatabase( - WasmSqlite3 sqlite3, String path, String vfs) async { - final asyncDb = await super.openDatabase(sqlite3, path, vfs); - setupPowerSyncDatabase(asyncDb.database); - return asyncDb; - } - - @override - Future handleCustomRequest( - ClientConnection connection, JSAny? request) { - throw UnimplementedError(); - } -} diff --git a/packages/powersync_core/lib/src/web/worker_utils.dart b/packages/powersync_core/lib/src/web/worker_utils.dart index da14808d..f2a5ac9a 100644 --- a/packages/powersync_core/lib/src/web/worker_utils.dart +++ b/packages/powersync_core/lib/src/web/worker_utils.dart @@ -1,7 +1,27 @@ -import 'package:powersync_core/sqlite3_common.dart'; +import 'dart:js_interop'; + import 'package:powersync_core/src/open_factory/common_db_functions.dart'; +import 'package:sqlite_async/sqlite3_wasm.dart'; +import 'package:sqlite_async/sqlite3_web.dart'; +import 'package:sqlite_async/sqlite3_web_worker.dart'; import 'package:uuid/uuid.dart'; +final class PowerSyncAsyncSqliteController extends AsyncSqliteController { + @override + Future openDatabase( + WasmSqlite3 sqlite3, String path, String vfs) async { + final asyncDb = await super.openDatabase(sqlite3, path, vfs); + setupPowerSyncDatabase(asyncDb.database); + return asyncDb; + } + + @override + Future handleCustomRequest( + ClientConnection connection, JSAny? request) { + throw UnimplementedError(); + } +} + // Registers custom SQLite functions for the SQLite connection void setupPowerSyncDatabase(CommonDatabase database) { setupCommonDBFunctions(database);