| 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 'invoker.dart'; |
| 8 | | | import 'tokens/_squadron_cancelation_token.dart'; |
| 9 | | | import 'typedefs.dart'; |
| 10 | | | import 'worker/worker.dart'; |
| 11 | | | import 'worker/worker_request.dart'; |
| 12 | | | import 'worker_service.dart'; |
| 13 | | | |
| 14 | | | @Deprecated('Use WorkerClient instead.') |
| 15 | | | typedef LocalWorkerClient = WorkerClient; |
| 16 | | | |
| 17 | | | /// Base class used to communicate with a [Worker] over a [Channel]. |
| 18 | | | /// |
| 19 | | | /// Typically, derived classes should add proxy methods sending [WorkerRequest]s to the worker. |
| 20 | | | class WorkerClient with Releasable implements WorkerService, Invoker { |
| 21 | | | /// Create a client for a [Worker]. The [channel] passed to this client must have been obtained |
| 22 | | | /// from the target [Worker]. |
| 23 | | 1 | WorkerClient(this.channel); |
| 24 | | | |
| 25 | | | /// The [Channel] to communicate with the [Worker]. |
| 26 | | | final Channel channel; |
| 27 | | | |
| 28 | | 0 | @override |
| 29 | | | void release() { |
| 30 | | 0 | channel.close(); |
| 31 | | 0 | super.release(); |
| 32 | | | } |
| 33 | | | |
| 34 | | | /// Sends a command to the [Worker]. |
| 35 | | 1 | @override |
| 36 | | | Future<dynamic> send(int command, |
| 37 | | | {List args = const [], |
| 38 | | | CancelationToken? token, |
| 39 | | | bool inspectRequest = false, |
| 40 | | | bool inspectResponse = false}) => |
| 41 | | 2 | channel.sendRequest(command, args, |
| 42 | | 0 | token: token?.wrap(), |
| 43 | | | inspectRequest: inspectRequest, |
| 44 | | | inspectResponse: inspectResponse); |
| 45 | | | |
| 46 | | | /// Sends a streaming command to the [Worker]. |
| 47 | | 1 | @override |
| 48 | | | Stream<dynamic> stream(int command, |
| 49 | | | {List args = const [], |
| 50 | | | CancelationToken? token, |
| 51 | | | bool inspectRequest = false, |
| 52 | | | bool inspectResponse = false}) => |
| 53 | | 2 | channel.sendStreamingRequest(command, args, |
| 54 | | 0 | token: token?.wrap(), |
| 55 | | | inspectRequest: inspectRequest, |
| 56 | | | inspectResponse: inspectResponse); |
| 57 | | | |
| 58 | | | /// Local worker clients do not need an [operations] map. |
| 59 | | | @override |
| 60 | | | final OperationsMap operations = WorkerService.noOperations; |
| 61 | | | } |