1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import 'package:logger/web.dart'; |
4 | | | import 'package:meta/meta.dart'; |
5 | | |
|
6 | | | import '_impl/xplat/_channel.dart' |
7 | | | if (dart.library.io) '_impl/native/_channel.dart' |
8 | | | if (dart.library.html) '_impl/web/_channel.dart' |
9 | | | if (dart.library.js) '_impl/web/_channel.dart' |
10 | | | if (dart.library.js_interop) '_impl/web/_channel.dart' as impl; |
11 | | | import 'exceptions/exception_manager.dart'; |
12 | | | import 'exceptions/task_terminated_exception.dart'; |
13 | | | import 'tokens/_squadron_cancelation_token.dart'; |
14 | | | import 'typedefs.dart'; |
15 | | | import 'worker/worker_request.dart'; |
16 | | | import 'worker/worker_response.dart'; |
17 | | |
|
18 | | | /// A [Channel] supports communication from a client to a platform worker. It |
19 | | | /// is used to send a [WorkerRequest] to a platform worker. |
20 | | | abstract interface class Channel { |
21 | | | /// The [ExceptionManager] attached to this channel. |
22 | | | ExceptionManager get exceptionManager; |
23 | | |
|
24 | | | /// The [Logger] attached to this channel. |
25 | | | Logger? get logger; |
26 | | |
|
27 | | | /// [Channel] serialization. Returns an opaque object that can be transfered |
28 | | | /// from the client to the worker. |
29 | | | PlatformChannel serialize(); |
30 | | |
|
31 | | | /// [Channel] sharing. Returns a [Channel] object that can be provided to |
32 | | | /// enable another worker to call the channel's worker. |
33 | | | Channel share(); |
34 | | |
|
35 | | | /// Sends a termination [WorkerRequest] to the worker. The [Channel] should |
36 | | | /// release any resource related to the worker and should not be used after |
37 | | | /// this method has been called. |
38 | | | FutureOr<void> close(); |
39 | | |
|
40 | | | /// Sends a close stream [WorkerRequest] to the worker. |
41 | | | FutureOr<void> cancelStream(int streamId); |
42 | | |
|
43 | | | /// Sends a cancel token [WorkerRequest] to the worker. |
44 | | | FutureOr<void> cancelToken(SquadronCancelationToken? token); |
45 | | |
|
46 | | | /// Creates a [WorkerRequest] and sends it to the worker. This method expects |
47 | | | /// a single value from the worker. |
48 | | | Future<dynamic> sendRequest(int command, List args, |
49 | | | {SquadronCancelationToken? token, |
50 | | | bool inspectRequest = false, |
51 | | | bool inspectResponse = false}); |
52 | | |
|
53 | | | /// Creates a [WorkerRequest] and sends it to the worker. This method expects |
54 | | | /// a stream of values from the worker. The worker must send a |
55 | | | /// [WorkerResponse.closeStream] message to close the [Stream]. |
56 | | | Stream<dynamic> sendStreamingRequest(int command, List args, |
57 | | | {SquadronCancelationToken? token, |
58 | | | bool inspectRequest = false, |
59 | | | bool inspectResponse = false}); |
60 | | |
|
61 | | | /// Starts a worker using the [entryPoint] and sends a start [WorkerRequest] |
62 | | | /// with [startArguments]. The future must not complete before the worker is |
63 | | | /// ready to serve requests. |
64 | | 11 | static Future<Channel> open( |
65 | | | ExceptionManager exceptionManager, |
66 | | | Logger? logger, |
67 | | | EntryPoint entryPoint, |
68 | | | List startArguments, |
69 | | | PlatformThreadHook? hook, |
70 | | | ) => |
71 | | 11 | impl.openChannel( |
72 | | | entryPoint, exceptionManager, logger, startArguments, hook); |
73 | | |
|
74 | | | /// Deserializes a [Channel] from an opaque [channelInfo]. |
75 | | 3 | static Channel? deserialize( |
76 | | | PlatformChannel? channelInfo, [ |
77 | | | Logger? logger, |
78 | | | ExceptionManager? exceptionManager, |
79 | | | ]) => |
80 | | 3 | impl.deserialize(channelInfo, logger, exceptionManager); |
81 | | | } |
82 | | |
|
83 | | | @internal |
84 | | | extension ChannelTerminationImpl on Channel { |
85 | | 2 | void terminate(TaskTerminatedException ex) => impl.terminateChannel(this, ex); |
86 | | | } |