Skip to content

Commit

Permalink
Web: Enable PowerSync extensions even without worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Binder committed Jan 16, 2025
1 parent bd22580 commit 4ba63ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebSqlite> 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
Expand Down
20 changes: 0 additions & 20 deletions packages/powersync_core/lib/src/web/powersync_db.worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<WorkerDatabase> openDatabase(
WasmSqlite3 sqlite3, String path, String vfs) async {
final asyncDb = await super.openDatabase(sqlite3, path, vfs);
setupPowerSyncDatabase(asyncDb.database);
return asyncDb;
}

@override
Future<JSAny?> handleCustomRequest(
ClientConnection connection, JSAny? request) {
throw UnimplementedError();
}
}
22 changes: 21 additions & 1 deletion packages/powersync_core/lib/src/web/worker_utils.dart
Original file line number Diff line number Diff line change
@@ -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<WorkerDatabase> openDatabase(
WasmSqlite3 sqlite3, String path, String vfs) async {
final asyncDb = await super.openDatabase(sqlite3, path, vfs);
setupPowerSyncDatabase(asyncDb.database);
return asyncDb;
}

@override
Future<JSAny?> handleCustomRequest(
ClientConnection connection, JSAny? request) {
throw UnimplementedError();
}
}

// Registers custom SQLite functions for the SQLite connection
void setupPowerSyncDatabase(CommonDatabase database) {
setupCommonDBFunctions(database);
Expand Down

0 comments on commit 4ba63ab

Please sign in to comment.