2
2
library sqlite_async.web;
3
3
4
4
import 'package:sqlite3_web/sqlite3_web.dart' ;
5
+ import 'package:web/web.dart' ;
5
6
import 'sqlite_async.dart' ;
6
7
import 'src/web/database.dart' ;
7
8
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
+
8
24
/// A [SqliteConnection] interface implemented by opened connections when
9
25
/// running on the web.
10
26
///
@@ -13,23 +29,30 @@ import 'src/web/database.dart';
13
29
/// opened database across different JavaScript contexts
14
30
/// (e.g. document windows and workers).
15
31
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.
19
34
///
20
35
/// After transferring this endpoint to another JavaScript context (e.g. a
21
36
/// worker), the worker can call [connectToEndpoint] to obtain a connection to
22
37
/// the same sqlite database.
23
- Future <SqliteWebEndpoint > exposeEndpoint ();
38
+ Future <WebDatabaseEndpoint > exposeEndpoint ();
24
39
25
40
/// Connect to an endpoint obtained through [exposeEndpoint] .
26
41
///
27
42
/// The endpoint is transferrable in JavaScript, allowing multiple JavaScript
28
43
/// contexts to exchange opened database connections.
29
44
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
+ );
33
56
return database;
34
57
}
35
58
}
0 commit comments