@@ -4,6 +4,7 @@ import 'dart:js_interop_unsafe';
44import 'dart:typed_data' ;
55
66import 'package:sqlite3/wasm.dart' hide WorkerOptions;
7+ import 'package:stream_channel/stream_channel.dart' ;
78import 'package:web/web.dart'
89 hide Response, Request, FileSystem, Notification, Lock;
910
@@ -257,15 +258,21 @@ final class RemoteFileSystem implements FileSystem {
257258final class WorkerConnection extends ProtocolChannel {
258259 final StreamController <Notification > notifications =
259260 StreamController .broadcast ();
261+ final Future <JSAny ?> Function (JSAny ? ) handleCustomRequest;
260262
261- WorkerConnection (super .channel) {
263+ WorkerConnection (super .channel, this .handleCustomRequest ) {
262264 closed.whenComplete (notifications.close);
263265 }
264266
265267 @override
266- Future <Response > handleRequest (Request request) {
267- // TODO: implement handleRequest
268- throw UnimplementedError ();
268+ Future <Response > handleRequest (Request request) async {
269+ switch (request) {
270+ case CustomRequest (requestId: final id, : final payload):
271+ final response = await handleCustomRequest (payload);
272+ return SimpleSuccessResponse (response: response, requestId: id);
273+ default :
274+ throw UnimplementedError ();
275+ }
269276 }
270277
271278 @override
@@ -278,6 +285,7 @@ final class DatabaseClient implements WebSqlite {
278285 final Uri workerUri;
279286 final Uri wasmUri;
280287 final DatabaseController _localController;
288+ final Future <JSAny ?> Function (JSAny ? ) _handleCustomRequest;
281289
282290 final Lock _startWorkersLock = Lock ();
283291 bool _startedWorkers = false ;
@@ -289,7 +297,12 @@ final class DatabaseClient implements WebSqlite {
289297
290298 final Set <MissingBrowserFeature > _missingFeatures = {};
291299
292- DatabaseClient (this .workerUri, this .wasmUri, this ._localController);
300+ DatabaseClient (this .workerUri, this .wasmUri, this ._localController,
301+ Future <JSAny ?> Function (JSAny ? )? handleCustomRequest)
302+ : _handleCustomRequest = handleCustomRequest ??
303+ ((_) async {
304+ throw StateError ('No custom request handler installed' );
305+ });
293306
294307 Future <void > startWorkers () {
295308 return _startWorkersLock.synchronized (() async {
@@ -303,6 +316,10 @@ final class DatabaseClient implements WebSqlite {
303316 });
304317 }
305318
319+ WorkerConnection _connection (StreamChannel <Message > channel) {
320+ return WorkerConnection (channel, _handleCustomRequest);
321+ }
322+
306323 Future <void > _startDedicated () async {
307324 if (globalContext.has ('Worker' )) {
308325 final Worker dedicated;
@@ -319,8 +336,7 @@ final class DatabaseClient implements WebSqlite {
319336 final (endpoint, channel) = await createChannel ();
320337 ConnectRequest (endpoint: endpoint, requestId: 0 ).sendToWorker (dedicated);
321338
322- _connectionToDedicated =
323- WorkerConnection (channel.injectErrorsFrom (dedicated));
339+ _connectionToDedicated = _connection (channel.injectErrorsFrom (dedicated));
324340 } else {
325341 _missingFeatures.add (MissingBrowserFeature .dedicatedWorkers);
326342 }
@@ -341,7 +357,7 @@ final class DatabaseClient implements WebSqlite {
341357 final (endpoint, channel) = await createChannel ();
342358 ConnectRequest (endpoint: endpoint, requestId: 0 ).sendToPort (shared.port);
343359
344- _connectionToShared = WorkerConnection (channel.injectErrorsFrom (shared));
360+ _connectionToShared = _connection (channel.injectErrorsFrom (shared));
345361 } else {
346362 _missingFeatures.add (MissingBrowserFeature .sharedWorkers);
347363 }
@@ -358,7 +374,7 @@ final class DatabaseClient implements WebSqlite {
358374 ConnectRequest (requestId: 0 , endpoint: endpoint),
359375 MessageType .simpleSuccessResponse);
360376
361- return _connectionToDedicatedInShared = WorkerConnection (channel);
377+ return _connectionToDedicatedInShared = _connection (channel);
362378 });
363379 }
364380
@@ -374,7 +390,7 @@ final class DatabaseClient implements WebSqlite {
374390 local
375391 .addTopLevelMessage (ConnectRequest (requestId: 0 , endpoint: endpoint));
376392
377- return _connectionToLocal = WorkerConnection (channel);
393+ return _connectionToLocal = _connection (channel);
378394 });
379395 }
380396
@@ -512,7 +528,7 @@ final class DatabaseClient implements WebSqlite {
512528 }
513529
514530 Future <Database > connectToExisting (SqliteWebEndpoint endpoint) async {
515- final channel = WorkerConnection (
531+ final channel = _connection (
516532 WebEndpoint (port: endpoint.$1, lockName: endpoint.$2).connect ());
517533
518534 return RemoteDatabase (
0 commit comments