@@ -4,6 +4,7 @@ import 'dart:js_interop_unsafe';
4
4
import 'dart:typed_data' ;
5
5
6
6
import 'package:sqlite3/wasm.dart' hide WorkerOptions;
7
+ import 'package:stream_channel/stream_channel.dart' ;
7
8
import 'package:web/web.dart'
8
9
hide Response, Request, FileSystem, Notification, Lock;
9
10
@@ -257,15 +258,21 @@ final class RemoteFileSystem implements FileSystem {
257
258
final class WorkerConnection extends ProtocolChannel {
258
259
final StreamController <Notification > notifications =
259
260
StreamController .broadcast ();
261
+ final Future <JSAny ?> Function (JSAny ? ) handleCustomRequest;
260
262
261
- WorkerConnection (super .channel) {
263
+ WorkerConnection (super .channel, this .handleCustomRequest ) {
262
264
closed.whenComplete (notifications.close);
263
265
}
264
266
265
267
@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
+ }
269
276
}
270
277
271
278
@override
@@ -278,6 +285,7 @@ final class DatabaseClient implements WebSqlite {
278
285
final Uri workerUri;
279
286
final Uri wasmUri;
280
287
final DatabaseController _localController;
288
+ final Future <JSAny ?> Function (JSAny ? ) _handleCustomRequest;
281
289
282
290
final Lock _startWorkersLock = Lock ();
283
291
bool _startedWorkers = false ;
@@ -289,7 +297,12 @@ final class DatabaseClient implements WebSqlite {
289
297
290
298
final Set <MissingBrowserFeature > _missingFeatures = {};
291
299
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
+ });
293
306
294
307
Future <void > startWorkers () {
295
308
return _startWorkersLock.synchronized (() async {
@@ -303,6 +316,10 @@ final class DatabaseClient implements WebSqlite {
303
316
});
304
317
}
305
318
319
+ WorkerConnection _connection (StreamChannel <Message > channel) {
320
+ return WorkerConnection (channel, _handleCustomRequest);
321
+ }
322
+
306
323
Future <void > _startDedicated () async {
307
324
if (globalContext.has ('Worker' )) {
308
325
final Worker dedicated;
@@ -319,8 +336,7 @@ final class DatabaseClient implements WebSqlite {
319
336
final (endpoint, channel) = await createChannel ();
320
337
ConnectRequest (endpoint: endpoint, requestId: 0 ).sendToWorker (dedicated);
321
338
322
- _connectionToDedicated =
323
- WorkerConnection (channel.injectErrorsFrom (dedicated));
339
+ _connectionToDedicated = _connection (channel.injectErrorsFrom (dedicated));
324
340
} else {
325
341
_missingFeatures.add (MissingBrowserFeature .dedicatedWorkers);
326
342
}
@@ -341,7 +357,7 @@ final class DatabaseClient implements WebSqlite {
341
357
final (endpoint, channel) = await createChannel ();
342
358
ConnectRequest (endpoint: endpoint, requestId: 0 ).sendToPort (shared.port);
343
359
344
- _connectionToShared = WorkerConnection (channel.injectErrorsFrom (shared));
360
+ _connectionToShared = _connection (channel.injectErrorsFrom (shared));
345
361
} else {
346
362
_missingFeatures.add (MissingBrowserFeature .sharedWorkers);
347
363
}
@@ -358,7 +374,7 @@ final class DatabaseClient implements WebSqlite {
358
374
ConnectRequest (requestId: 0 , endpoint: endpoint),
359
375
MessageType .simpleSuccessResponse);
360
376
361
- return _connectionToDedicatedInShared = WorkerConnection (channel);
377
+ return _connectionToDedicatedInShared = _connection (channel);
362
378
});
363
379
}
364
380
@@ -374,7 +390,7 @@ final class DatabaseClient implements WebSqlite {
374
390
local
375
391
.addTopLevelMessage (ConnectRequest (requestId: 0 , endpoint: endpoint));
376
392
377
- return _connectionToLocal = WorkerConnection (channel);
393
+ return _connectionToLocal = _connection (channel);
378
394
});
379
395
}
380
396
@@ -512,7 +528,7 @@ final class DatabaseClient implements WebSqlite {
512
528
}
513
529
514
530
Future <Database > connectToExisting (SqliteWebEndpoint endpoint) async {
515
- final channel = WorkerConnection (
531
+ final channel = _connection (
516
532
WebEndpoint (port: endpoint.$1, lockName: endpoint.$2).connect ());
517
533
518
534
return RemoteDatabase (
0 commit comments