| 1 | | | part of '_channel.dart'; |
| 2 | | | |
| 3 | | | /// [Channel] used to communicate between [Isolates]s. Creates a |
| 4 | | | /// [vm.ReceivePort] to receive commands and forwards them to the worker's [vm.SendPort]. |
| 5 | | | final class _VmForwardChannel extends _VmChannel { |
| 6 | | | /// [_remote] is the worker's [web.MessagePort], [_com] is the intermediate |
| 7 | | | /// message channel |
| 8 | | 3 | _VmForwardChannel._(this._remote, this._com, Logger? logger, |
| 9 | | | ExceptionManager exceptionManager) |
| 10 | | 6 | : super._(_com.sendPort, logger, exceptionManager) { |
| 11 | | 9 | _com.listen(_forward); |
| 12 | | | } |
| 13 | | | |
| 14 | | | /// [web.MessagePort] to the worker. |
| 15 | | | final vm.SendPort _remote; |
| 16 | | | |
| 17 | | | /// [web.MessageChannel] used for forwarding messages. |
| 18 | | | final vm.ReceivePort _com; |
| 19 | | | |
| 20 | | | /// Forwards [data] to the worker. |
| 21 | | 3 | void _forward(dynamic data) { |
| 22 | | 3 | if (_closed) { |
| 23 | | 0 | throw SquadronErrorImpl.create('Channel is closed'); |
| 24 | | | } |
| 25 | | 6 | _remote.send(data); |
| 26 | | | } |
| 27 | | | |
| 28 | | | /// Closes this [Channel], effectively stopping message forwarding. |
| 29 | | 0 | @override |
| 30 | | | void close() { |
| 31 | | 0 | if (!_closed) { |
| 32 | | 0 | _com.close(); |
| 33 | | 0 | _closed = true; |
| 34 | | | } |
| 35 | | | } |
| 36 | | | } |