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