1 | | | import 'dart:async'; |
2 | | | import 'dart:js_interop'; |
3 | | |
|
4 | | | import 'package:web/web.dart' as web; |
5 | | |
|
6 | | | import '../../channel.dart'; |
7 | | | import '../../exceptions/exception_manager.dart'; |
8 | | | import '../../local_worker/local_worker.dart'; |
9 | | | import '../../worker_service.dart'; |
10 | | | import '../xplat/_worker_runner.dart'; |
11 | | | import '_worker_runner.dart'; |
12 | | |
|
13 | | | base class _WebLocalWorker<W extends WorkerService> extends LocalWorker<W> { |
14 | | 1 | _WebLocalWorker._(super.service, ExceptionManager exceptionManager) { |
15 | | 1 | final runner = WorkerRunner.use(this); |
16 | | 1 | _port.port1.onmessage = runner.handle.toJS; |
17 | | | _channel = Channel.deserialize( |
18 | | 1 | _port.port2, runner.internalLogger, exceptionManager); |
19 | | | } |
20 | | |
|
21 | | | final _port = web.MessageChannel(); |
22 | | 1 | late Channel? _channel; |
23 | | |
|
24 | | | @override |
25 | | | Channel? get channel => _channel; |
26 | | |
|
27 | | | @override |
28 | | | void stop() { |
29 | | 1 | _port.port1.close(); |
30 | | | _port.port2.close(); |
31 | | 1 | _channel?.close(); |
32 | | | _channel = null; |
33 | | | } |
34 | | |
|
35 | | | @override |
36 | | 1 | ExceptionManager get exceptionManager => |
37 | | 1 | (_exceptionManager ??= ExceptionManager()); |
38 | | | ExceptionManager? _exceptionManager; |
39 | | |
|
40 | | | @override |
41 | | | FutureOr<void> start() {} |
42 | | | } |
43 | | |
|
44 | | | /// Creates a [LocalWorker] on a browser platform. |
45 | | | LocalWorker<W> createLocalWorker<W extends WorkerService>( |
46 | | | W service, ExceptionManager exceptionManager) => |
47 | | | _WebLocalWorker._(service, exceptionManager); |