Skip to content

Commit 7b4e2a7

Browse files
committed
Support running without web workers
1 parent 4e183f3 commit 7b4e2a7

File tree

6 files changed

+57
-24
lines changed

6 files changed

+57
-24
lines changed

packages/sqlite_async/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.11.1
2+
3+
- Remove remaining `dart:js_util` imports in favor of new interop APIs.
4+
- Add `WebSqliteOpenFactory` with web-specific behavior for open factories.
5+
16
## 0.11.0
27

38
- Automatically flush IndexedDB storage to fix durability issues

packages/sqlite_async/lib/src/web/web_mutex.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:math';
44
import 'package:meta/meta.dart';
55
import 'package:mutex/mutex.dart' as mutex;
66
import 'dart:js_interop';
7-
import 'dart:js_util' as js_util;
87
// This allows for checking things like hasProperty without the need for depending on the `js` package
98
import 'dart:js_interop_unsafe';
109
import 'package:web/web.dart';
@@ -128,7 +127,7 @@ class MutexImpl implements Mutex {
128127
.request(resolvedIdentifier, lockOptions, jsCallback.toJS);
129128
// A timeout abort will throw an exception which needs to be handled.
130129
// There should not be any other unhandled lock errors.
131-
js_util.promiseToFuture(promise).catchError((error) {});
130+
promise.toDart.catchError((error) => null);
132131
return gotLock.future;
133132
}
134133

packages/sqlite_async/lib/src/web/web_sqlite_open_factory.dart

+29-17
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,45 @@ import 'package:sqlite3_web/sqlite3_web.dart';
55
import 'package:sqlite_async/sqlite_async.dart';
66
import 'package:sqlite_async/src/web/database/broadcast_updates.dart';
77
import 'package:sqlite_async/src/web/web_mutex.dart';
8+
import 'package:sqlite_async/web.dart';
89

910
import 'database.dart';
11+
import 'worker/worker_utils.dart';
1012

1113
Map<String, FutureOr<WebSqlite>> webSQLiteImplementations = {};
1214

1315
/// Web implementation of [AbstractDefaultSqliteOpenFactory]
1416
class DefaultSqliteOpenFactory
15-
extends AbstractDefaultSqliteOpenFactory<CommonDatabase> {
16-
final Future<WebSqlite> _initialized;
17+
extends AbstractDefaultSqliteOpenFactory<CommonDatabase>
18+
implements WebSqliteOpenFactory {
19+
late final Future<WebSqlite> _initialized = Future.sync(() {
20+
final cacheKey = sqliteOptions.webSqliteOptions.wasmUri +
21+
sqliteOptions.webSqliteOptions.workerUri;
22+
23+
if (webSQLiteImplementations.containsKey(cacheKey)) {
24+
return webSQLiteImplementations[cacheKey]!;
25+
}
26+
27+
webSQLiteImplementations[cacheKey] =
28+
openWebSqlite(sqliteOptions.webSqliteOptions);
29+
return webSQLiteImplementations[cacheKey]!;
30+
});
1731

1832
DefaultSqliteOpenFactory(
1933
{required super.path,
20-
super.sqliteOptions = const SqliteOptions.defaults()})
21-
: _initialized = Future.sync(() {
22-
final cacheKey = sqliteOptions.webSqliteOptions.wasmUri +
23-
sqliteOptions.webSqliteOptions.workerUri;
24-
25-
if (webSQLiteImplementations.containsKey(cacheKey)) {
26-
return webSQLiteImplementations[cacheKey]!;
27-
}
28-
29-
webSQLiteImplementations[cacheKey] = WebSqlite.open(
30-
wasmModule: Uri.parse(sqliteOptions.webSqliteOptions.wasmUri),
31-
worker: Uri.parse(sqliteOptions.webSqliteOptions.workerUri),
32-
);
33-
return webSQLiteImplementations[cacheKey]!;
34-
});
34+
super.sqliteOptions = const SqliteOptions.defaults()}) {
35+
// Make sure initializer starts running immediately
36+
_initialized;
37+
}
38+
39+
@override
40+
Future<WebSqlite> openWebSqlite(WebSqliteOptions options) async {
41+
return WebSqlite.open(
42+
wasmModule: Uri.parse(sqliteOptions.webSqliteOptions.wasmUri),
43+
worker: Uri.parse(sqliteOptions.webSqliteOptions.workerUri),
44+
controller: AsyncSqliteController(),
45+
);
46+
}
3547

3648
@override
3749

packages/sqlite_async/lib/src/web/worker/worker_utils.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:js_interop';
2-
import 'dart:js_util' as js_util;
32

43
import 'package:mutex/mutex.dart';
54
import 'package:sqlite3/wasm.dart';
@@ -73,7 +72,7 @@ class AsyncSqliteDatabase extends WorkerDatabase {
7372

7473
var dartMap = resultSetToMap(res);
7574

76-
var jsObject = js_util.jsify(dartMap);
75+
var jsObject = dartMap.jsify();
7776

7877
return jsObject;
7978
case CustomDatabaseMessageKind.executeBatchInTransaction:

packages/sqlite_async/lib/web.dart

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ library sqlite_async.web;
66

77
import 'package:sqlite3_web/sqlite3_web.dart';
88
import 'package:web/web.dart';
9+
10+
import 'sqlite3_common.dart';
911
import 'sqlite_async.dart';
1012
import 'src/web/database.dart';
1113

@@ -24,6 +26,22 @@ typedef WebDatabaseEndpoint = ({
2426
String? lockName,
2527
});
2628

29+
/// An additional interface for [SqliteOpenFactory] exposing additional
30+
/// functionality that is only relevant when compiling to the web.
31+
///
32+
/// The [DefaultSqliteOpenFactory] class implements this interface only when
33+
/// compiling for the web.
34+
abstract interface class WebSqliteOpenFactory
35+
implements SqliteOpenFactory<CommonDatabase> {
36+
/// Opens a [WebSqlite] instance for the given [options].
37+
///
38+
/// This method can be overriden in scenarios where the way [WebSqlite] is
39+
/// opened needs to be customized. Implementers should be aware that the
40+
/// result of this method is cached and will be re-used by the open factory
41+
/// when provided with the same [options] again.
42+
Future<WebSqlite> openWebSqlite(WebSqliteOptions options);
43+
}
44+
2745
/// A [SqliteConnection] interface implemented by opened connections when
2846
/// running on the web.
2947
///

packages/sqlite_async/pubspec.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: sqlite_async
22
description: High-performance asynchronous interface for SQLite on Dart and Flutter.
3-
version: 0.11.0
3+
version: 0.11.1
44
repository: https://github.com/powersync-ja/sqlite_async.dart
55
environment:
66
sdk: ">=3.4.0 <4.0.0"
@@ -12,8 +12,8 @@ topics:
1212
- flutter
1313

1414
dependencies:
15-
sqlite3: "^2.4.7"
16-
sqlite3_web: ^0.2.0
15+
sqlite3: ^2.6.0
16+
sqlite3_web: ^0.2.1
1717
async: ^2.10.0
1818
collection: ^1.17.0
1919
mutex: ^3.1.0

0 commit comments

Comments
 (0)