1 | | | import 'dart:html' as web; |
2 | | |
|
3 | | | import '../_worker_runner.dart'; |
4 | | | import '../channel.dart'; |
5 | | | import '../local_worker.dart'; |
6 | | | import '../worker_service.dart'; |
7 | | |
|
8 | | | class _JsLocalWorker<W extends WorkerService> extends LocalWorker<W> { |
9 | | 1 | _JsLocalWorker._(W service) : super(service) { |
10 | | | final runner = WorkerRunner.use(service); |
11 | | 1 | _port.port1.onMessage.listen((event) => runner.processMessage(event.data)); |
12 | | 1 | } |
13 | | |
|
14 | | | final _port = web.MessageChannel(); |
15 | | 1 | late Channel? _channel = Channel.deserialize(_port.port2); |
16 | | |
|
17 | | | @override |
18 | | 1 | Channel? get channel => _channel; |
19 | | |
|
20 | | | @override |
21 | | 1 | void stop() { |
22 | | 1 | _port.port1.close(); |
23 | | 1 | _port.port2.close(); |
24 | | 1 | _channel?.close(); |
25 | | | _channel = null; |
26 | | 1 | } |
27 | | | } |
28 | | |
|
29 | | | /// Creates a [LocalWorker] on a browser platform. |
30 | | | LocalWorker<W> createLocalWorker<W extends WorkerService>(W service) => |
31 | | 1 | _JsLocalWorker._(service); |