1 | | | import 'dart:async'; |
2 | | | import 'dart:isolate'; |
3 | | |
|
4 | | | import 'package:logger/web.dart'; |
5 | | | import 'package:meta/meta.dart'; |
6 | | |
|
7 | | | import '../../exceptions/squadron_error.dart'; |
8 | | | import '../../exceptions/squadron_exception.dart'; |
9 | | | import '../../exceptions/worker_exception.dart'; |
10 | | | import '../../typedefs.dart'; |
11 | | | import '../../worker/worker_channel.dart'; |
12 | | | import '../../worker/worker_response.dart'; |
13 | | |
|
14 | | | /// [WorkerChannel] implementation for the native world. |
15 | | | final class _VmWorkerChannel implements WorkerChannel { |
16 | | 9 | _VmWorkerChannel._(this._sendPort, this._logger); |
17 | | |
|
18 | | | final Logger? _logger; |
19 | | |
|
20 | | | /// [SendPort] to communicate with the [Isolate] if the channel is owned by |
21 | | | /// the worker owner. Otherwise, [SendPort] to return values to the client. |
22 | | | final SendPort _sendPort; |
23 | | |
|
24 | | 9 | void _postResponse(WorkerResponse res) { |
25 | | | try { |
26 | | 9 | final data = res.wrapInPlace(); |
27 | | 18 | _sendPort.send(data); |
28 | | | } catch (ex, st) { |
29 | | 4 | _logger?.e(() => 'Failed to post response $res: $ex'); |
30 | | 2 | throw SquadronErrorExt.create('Failed to post response: $ex', st); |
31 | | | } |
32 | | | } |
33 | | |
|
34 | | | /// Sends the [SendPort] to communicate with the [Isolate]. This method must |
35 | | | /// be called by the worker [Isolate] upon startup. |
36 | | 9 | @override |
37 | | 9 | void connect(PlatformChannel channelInfo) => inspectAndReply(channelInfo); |
38 | | |
|
39 | | | /// Sends a [WorkerResponse] with the specified data to the worker client. |
40 | | | /// This method must be called from the worker [Isolate] only. |
41 | | 9 | @override |
42 | | 18 | void reply(dynamic data) => _postResponse(WorkerResponse.withResult(data)); |
43 | | |
|
44 | | | /// Sends a [WorkerResponse] with the specified data to the worker client. |
45 | | | /// This method must be called from the worker [Isolate] only. On VM |
46 | | | /// platforms, this is the same as [reply] . |
47 | | 9 | @override |
48 | | 9 | void inspectAndReply(dynamic data) => reply(data); |
49 | | |
|
50 | | 9 | @override |
51 | | 18 | void log(LogEvent message) => _postResponse(WorkerResponse.log(message)); |
52 | | |
|
53 | | | /// Checks if [stream] can be streamed back to the worker client. Returns |
54 | | | /// `true` unless [stream] is a [ReceivePort]. |
55 | | 6 | @override |
56 | | 6 | bool canStream(Stream<dynamic> stream) => stream is! ReceivePort; |
57 | | |
|
58 | | | /// Sends a [WorkerResponse.closeStream] to the worker client. This method |
59 | | | /// must be called from the worker [Isolate] only. |
60 | | 6 | @override |
61 | | 12 | void closeStream() => _postResponse(WorkerResponse.closeStream()); |
62 | | |
|
63 | | | /// Sends the [WorkerException] to the worker client. This method must be |
64 | | | /// called from the worker [Isolate] only. |
65 | | 5 | @override |
66 | | | void error(Object err, [StackTrace? stackTrace, int? command]) { |
67 | | 5 | final ex = SquadronException.from(err, stackTrace, command); |
68 | | 10 | _postResponse(WorkerResponse.withError(ex)); |
69 | | | } |
70 | | | } |
71 | | |
|
72 | | | /// Creates a [WorkerChannel] from a [SendPort]. |
73 | | 9 | @internal |
74 | | | WorkerChannel? deserialize(PlatformChannel? channelInfo, [Logger? logger]) => |
75 | | 9 | (channelInfo == null) ? null : _VmWorkerChannel._(channelInfo, logger); |