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