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