|
| 1 | +/// |
| 2 | +library sqlite_async.web; |
| 3 | + |
| 4 | +import 'package:sqlite3_web/sqlite3_web.dart'; |
| 5 | +import 'sqlite_async.dart'; |
| 6 | +import 'src/web/database.dart'; |
| 7 | + |
| 8 | +/// A [SqliteConnection] interface implemented by opened connections when |
| 9 | +/// running on the web. |
| 10 | +/// |
| 11 | +/// This adds the [exposeEndpoint], which uses `dart:js_interop` types not |
| 12 | +/// supported on native Dart platforms. The method can be used to access an |
| 13 | +/// opened database across different JavaScript contexts |
| 14 | +/// (e.g. document windows and workers). |
| 15 | +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. |
| 19 | + /// |
| 20 | + /// After transferring this endpoint to another JavaScript context (e.g. a |
| 21 | + /// worker), the worker can call [connectToEndpoint] to obtain a connection to |
| 22 | + /// the same sqlite database. |
| 23 | + Future<SqliteWebEndpoint> exposeEndpoint(); |
| 24 | + |
| 25 | + /// Connect to an endpoint obtained through [exposeEndpoint]. |
| 26 | + /// |
| 27 | + /// The endpoint is transferrable in JavaScript, allowing multiple JavaScript |
| 28 | + /// contexts to exchange opened database connections. |
| 29 | + static Future<WebSqliteConnection> connectToEndpoint( |
| 30 | + SqliteWebEndpoint endpoint) async { |
| 31 | + final rawSqlite = await WebSqlite.connectToPort(endpoint); |
| 32 | + final database = WebDatabase(rawSqlite, null); |
| 33 | + return database; |
| 34 | + } |
| 35 | +} |
0 commit comments