1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import 'package:cancelation_token/cancelation_token.dart'; |
4 | | | import 'package:using/using.dart'; |
5 | | |
|
6 | | | import '../channel.dart'; |
7 | | | import '../tokens/_squadron_cancelation_token.dart'; |
8 | | | import '../worker/worker_request.dart'; |
9 | | | import '../worker_service.dart'; |
10 | | | import 'local_worker.dart'; |
11 | | |
|
12 | | | /// Base class used to communicate with a [LocalWorker]. |
13 | | | /// |
14 | | | /// Typically, derived classes should add proxy methods sending [WorkerRequest]s to the worker. |
15 | | | class LocalWorkerClient with Releasable implements WorkerService { |
16 | | | /// Create a client for a [LocalWorker]. The [channel] passed to this client must have been obtained by |
17 | | | /// calling [Channel.share] on the [LocalWorker.channel]. |
18 | | 1 | LocalWorkerClient(this.channel); |
19 | | |
|
20 | | | /// The [Channel] to communicate with the [LocalWorker]. |
21 | | | final Channel channel; |
22 | | |
|
23 | | 0 | @override |
24 | | | void release() { |
25 | | 0 | channel.close(); |
26 | | 0 | super.release(); |
27 | | | } |
28 | | |
|
29 | | | /// Sends a command to the [LocalWorker]. |
30 | | 1 | Future<dynamic> send(int command, |
31 | | | {List args = const [], |
32 | | | CancelationToken? token, |
33 | | | bool inspectRequest = false, |
34 | | | bool inspectResponse = false}) => |
35 | | 2 | channel.sendRequest(command, args, |
36 | | 0 | token: token?.wrap(), |
37 | | | inspectRequest: inspectRequest, |
38 | | | inspectResponse: inspectResponse); |
39 | | |
|
40 | | | /// Sends a streaming command to the [LocalWorker]. |
41 | | 1 | Stream<dynamic> stream(int command, |
42 | | | {List args = const [], |
43 | | | CancelationToken? token, |
44 | | | bool inspectRequest = false, |
45 | | | bool inspectResponse = false}) => |
46 | | 2 | channel.sendStreamingRequest(command, args, |
47 | | 0 | token: token?.wrap(), |
48 | | | inspectRequest: inspectRequest, |
49 | | | inspectResponse: inspectResponse); |
50 | | |
|
51 | | | /// Local worker clients do not need an [operations] map. |
52 | | | @override |
53 | | | final Map<int, CommandHandler> operations = WorkerService.noOperations; |
54 | | | } |