Skip to content

Commit 77039d0

Browse files
committed
Expose lock name as well
1 parent 79a33a7 commit 77039d0

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:sqlite_async/sqlite_async.dart';
77
import 'package:sqlite_async/src/utils/shared_utils.dart';
88
import 'package:sqlite_async/web.dart';
99
import 'protocol.dart';
10+
import 'web_mutex.dart';
1011

1112
class WebDatabase
1213
with SqliteQueries, SqliteDatabaseMixin
@@ -58,8 +59,17 @@ class WebDatabase
5859
Never get openFactory => throw UnimplementedError();
5960

6061
@override
61-
Future<SqliteWebEndpoint> exposeEndpoint() async {
62-
return await _database.additionalConnection();
62+
Future<WebDatabaseEndpoint> exposeEndpoint() async {
63+
final endpoint = await _database.additionalConnection();
64+
65+
return (
66+
connectPort: endpoint.$1,
67+
connectName: endpoint.$2,
68+
lockName: switch (_mutex) {
69+
MutexImpl(:final resolvedIdentifier) => resolvedIdentifier,
70+
_ => null,
71+
},
72+
);
6373
}
6474

6575
@override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ external Navigator get _navigator;
1818
class MutexImpl implements Mutex {
1919
late final mutex.Mutex fallback;
2020
String? identifier;
21-
final String _resolvedIdentifier;
21+
final String resolvedIdentifier;
2222

2323
MutexImpl({this.identifier})
2424

@@ -29,7 +29,7 @@ class MutexImpl implements Mutex {
2929
/// - The uuid package could be added for better uniqueness if required.
3030
/// This would add another package dependency to `sqlite_async` which is potentially unnecessary at this point.
3131
/// An identifier should be supplied for better exclusion.
32-
: _resolvedIdentifier = identifier ??
32+
: resolvedIdentifier = identifier ??
3333
"${DateTime.now().microsecondsSinceEpoch}-${Random().nextDouble()}" {
3434
fallback = mutex.Mutex();
3535
}
@@ -125,7 +125,7 @@ class MutexImpl implements Mutex {
125125
final lockOptions = JSObject();
126126
lockOptions['signal'] = controller.signal;
127127
final promise = _navigator.locks
128-
.request(_resolvedIdentifier, lockOptions, jsCallback.toJS);
128+
.request(resolvedIdentifier, lockOptions, jsCallback.toJS);
129129
// A timeout abort will throw an exception which needs to be handled.
130130
// There should not be any other unhandled lock errors.
131131
js_util.promiseToFuture(promise).catchError((error) {});

packages/sqlite_async/lib/web.dart

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@
22
library sqlite_async.web;
33

44
import 'package:sqlite3_web/sqlite3_web.dart';
5+
import 'package:web/web.dart';
56
import 'sqlite_async.dart';
67
import 'src/web/database.dart';
78

9+
/// An endpoint that can be used, by any running JavaScript context in the same
10+
/// website, to connect to an existing [WebSqliteConnection].
11+
///
12+
/// These endpoints are created by calling [WebSqliteConnection.exposeEndpoint]
13+
/// and consist of a [MessagePort] and two [String]s internally identifying the
14+
/// connection. Both objects can be transferred over send ports towards another
15+
/// worker or context. That context can then use
16+
/// [WebSqliteConnection.connectToEndpoint] to connect to the port already
17+
/// opened.
18+
typedef WebDatabaseEndpoint = ({
19+
MessagePort connectPort,
20+
String connectName,
21+
String? lockName,
22+
});
23+
824
/// A [SqliteConnection] interface implemented by opened connections when
925
/// running on the web.
1026
///
@@ -13,23 +29,30 @@ import 'src/web/database.dart';
1329
/// opened database across different JavaScript contexts
1430
/// (e.g. document windows and workers).
1531
abstract class WebSqliteConnection implements SqliteConnection {
16-
/// Returns a [SqliteWebEndpoint] from `package:sqlite3/web.dart` - a
17-
/// structure that consists only of types that can be transferred across a
18-
/// `MessagePort` in JavaScript.
32+
/// Returns a [WebDatabaseEndpoint] - a structure that consists only of types
33+
/// that can be transferred across a [MessagePort] in JavaScript.
1934
///
2035
/// After transferring this endpoint to another JavaScript context (e.g. a
2136
/// worker), the worker can call [connectToEndpoint] to obtain a connection to
2237
/// the same sqlite database.
23-
Future<SqliteWebEndpoint> exposeEndpoint();
38+
Future<WebDatabaseEndpoint> exposeEndpoint();
2439

2540
/// Connect to an endpoint obtained through [exposeEndpoint].
2641
///
2742
/// The endpoint is transferrable in JavaScript, allowing multiple JavaScript
2843
/// contexts to exchange opened database connections.
2944
static Future<WebSqliteConnection> connectToEndpoint(
30-
SqliteWebEndpoint endpoint) async {
31-
final rawSqlite = await WebSqlite.connectToPort(endpoint);
32-
final database = WebDatabase(rawSqlite, null);
45+
WebDatabaseEndpoint endpoint) async {
46+
final rawSqlite = await WebSqlite.connectToPort(
47+
(endpoint.connectPort, endpoint.connectName));
48+
49+
final database = WebDatabase(
50+
rawSqlite,
51+
switch (endpoint.lockName) {
52+
var lock? => Mutex(identifier: lock),
53+
null => null,
54+
},
55+
);
3356
return database;
3457
}
3558
}

0 commit comments

Comments
 (0)