22library sqlite_async.web;
33
44import 'package:sqlite3_web/sqlite3_web.dart' ;
5+ import 'package:web/web.dart' ;
56import 'sqlite_async.dart' ;
67import '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).
1531abstract 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