1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import 'cancellation_token.dart'; |
4 | | | import '_channel_stub.dart' |
5 | | | if (dart.library.js) 'browser/_channel.dart' |
6 | | | if (dart.library.html) 'browser/_channel.dart' |
7 | | | if (dart.library.io) 'native/_channel.dart'; |
8 | | |
|
9 | | | import 'squadron_exception.dart'; |
10 | | | import 'worker_request.dart'; |
11 | | | import 'worker_response.dart'; |
12 | | |
|
13 | | | /// A [Channel] supports communication from a client to a platform worker. |
14 | | | /// It is used to send a [WorkerRequest] to a platform worker. |
15 | | | abstract class Channel { |
16 | | | /// [Channel] serialization. |
17 | | | /// Returns an opaque object that can be transfered from the client to the worker. |
18 | | | dynamic serialize(); |
19 | | |
|
20 | | | /// [Channel] sharing. |
21 | | | /// Returns a [Channel] object that can be provided to enable another worker to call the channel's worker. |
22 | | | Channel share(); |
23 | | |
|
24 | | | /// Sends a termination [WorkerRequest] to the worker. |
25 | | | /// The [Channel] should release any resource related to the worker and should not be used after this method has been called. |
26 | | | FutureOr close(); |
27 | | |
|
28 | | | /// Creates a [WorkerRequest] and sends it to the worker. |
29 | | | /// This method expects a single value from the worker. |
30 | | | void notifyCancellation(CancellationToken cancelToken); |
31 | | |
|
32 | | | /// Creates a [WorkerRequest] and sends it to the worker. |
33 | | | /// This method expects a single value from the worker. |
34 | | | Future<T> sendRequest<T>(int command, List args, {CancellationToken? token}); |
35 | | |
|
36 | | | /// Creates a [WorkerRequest] and sends it to the worker. |
37 | | | /// This method expects a stream of values from the worker. |
38 | | | /// The worker must send a [WorkerResponse.closeStream] message to close the [Stream]. |
39 | | | Stream<T> sendStreamingRequest<T>(int command, List args, |
40 | | | {CancellationToken? token}); |
41 | | |
|
42 | | | /// Starts a worker using the [entryPoint] and sends a start [WorkerRequest] with [startArguments]. |
43 | | | /// The future must not complete before the worker is ready to serve requests. |
44 | | 1 | static Future<Channel> open(dynamic entryPoint, List startArguments) => |
45 | | 2 | openChannel(entryPoint, startArguments); |
46 | | |
|
47 | | | /// Deserializes a [Channel] from an opaque [channelInfo]. |
48 | | 1 | static Channel? deserialize(dynamic channelInfo) => |
49 | | 1 | deserializeChannel(channelInfo); |
50 | | | } |
51 | | |
|
52 | | | /// A [WorkerChannel] supports communication from a platform worker to the client that posted the [WorkerRequest]. |
53 | | | /// It is used to send [WorkerResponse] back to the client. |
54 | | | abstract class WorkerChannel { |
55 | | | /// [WorkerChannel] serialization. |
56 | | | /// Returns an opaque object that can be transfered from the client to the worker. |
57 | | | dynamic serialize(); |
58 | | |
|
59 | | | /// Connects the [Channel] with the Squadron [Worker]. |
60 | | | /// [channelInfo] is an opaque object than can be deserialized as a [Channel]. |
61 | | | /// This method must be called by the worker upon startup. |
62 | | | void connect(Object channelInfo); |
63 | | |
|
64 | | | /// Sends a [WorkerResponse] with the specified data to the worker client. |
65 | | | /// This method must be called from the worker only. |
66 | | | void reply(dynamic data); |
67 | | |
|
68 | | | /// Sends a [WorkerResponse.closeStream] to the worker client. |
69 | | | /// This method must be called from the worker only. |
70 | | | void closeStream(); |
71 | | |
|
72 | | | /// Sends a [WorkerResponse] with the specified error to the worker client. |
73 | | | /// This method must be called from the worker only. |
74 | | | void error(SquadronException error); |
75 | | |
|
76 | | | /// Deserializes a [Channel] from an opaque [channelInfo]. |
77 | | 1 | static WorkerChannel? deserialize(dynamic channelInfo) => |
78 | | 2 | deserializeWorkerChannel(channelInfo); |
79 | | | } |