| 1 | | | import 'dart:async'; |
| 2 | | | |
| 3 | | | import 'package:logger/web.dart'; |
| 4 | | | |
| 5 | | | import '../_impl/xplat/_worker_channel.dart' |
| 6 | | | if (dart.library.io) '../_impl/native/_worker_channel.dart' |
| 7 | | | if (dart.library.html) '../_impl/web/_worker_channel.dart' |
| 8 | | | if (dart.library.js) '../_impl/web/_worker_channel.dart' |
| 9 | | | if (dart.library.js_interop) '../_impl/web/_worker_channel.dart' as impl; |
| 10 | | | import '../channel.dart'; |
| 11 | | | import '../typedefs.dart'; |
| 12 | | | import 'worker.dart'; |
| 13 | | | import 'worker_request.dart'; |
| 14 | | | import 'worker_response.dart'; |
| 15 | | | |
| 16 | | | typedef PostRequest = void Function(WorkerRequest req); |
| 17 | | | |
| 18 | | | /// A [WorkerChannel] supports communication from a platform worker to the |
| 19 | | | /// client that posted the [WorkerRequest]. It is used to send [WorkerResponse] |
| 20 | | | /// back to the client. |
| 21 | | | abstract interface class WorkerChannel { |
| 22 | | | /// Connects the [Channel] with the Squadron [Worker]. [channelInfo] is an |
| 23 | | | /// opaque object than can be deserialized as a [Channel]. This method must |
| 24 | | | /// be called by the worker upon startup. |
| 25 | | | void connect(PlatformChannel channelInfo); |
| 26 | | | |
| 27 | | | /// Sends a [WorkerResponse] with the specified data to the worker client. |
| 28 | | | /// This method must be called from the worker only. On Web patforms, this |
| 29 | | | /// version does not check arguments for transferable objects. |
| 30 | | | void reply(dynamic data); |
| 31 | | | |
| 32 | | | /// Sends a [WorkerResponse] with the specified data to the worker client. |
| 33 | | | /// This method must be called from the worker only. On Web patforms, this |
| 34 | | | /// version checks arguments for transferable objects. |
| 35 | | | void inspectAndReply(dynamic data); |
| 36 | | | |
| 37 | | | /// Sends a [WorkerResponse.log] with the specified data to the worker |
| 38 | | | /// client. |
| 39 | | | void log(LogEvent message); |
| 40 | | | |
| 41 | | | /// Checks if [stream] can be streamed back to the worker client. |
| 42 | | | bool canStream(Stream<dynamic> stream); |
| 43 | | | |
| 44 | | | /// Sends a [WorkerResponse.closeStream] to the worker client. |
| 45 | | | void closeStream(); |
| 46 | | | |
| 47 | | | /// Sends a [WorkerResponse] with the specified error to the worker client. |
| 48 | | | void error(Object err, [StackTrace? stacktrace, int? command]); |
| 49 | | | |
| 50 | | | /// Deserializes a [Channel] from an opaque [channelInfo]. |
| 51 | | 10 | static WorkerChannel? deserialize(PlatformChannel? channelInfo, |
| 52 | | | [Logger? logger]) => |
| 53 | | 10 | impl.deserialize(channelInfo, logger); |
| 54 | | | } |