1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import 'squadron_exception.dart'; |
4 | | |
|
5 | | | /// Class used to communicate from a [Worker] to clients. |
6 | | | /// [WorkerResponse]s are used to provide individual results to the client. [Future]-based services simply return a |
7 | | | /// single [WorkerResponse] with the result. [Stream]ing services will return one [WorkerResponse]s for each stream |
8 | | | /// item and mmust send a [WorkerResponse.endOfStream] message to indicate completion. [WorkerResponse]s can also |
9 | | | /// send error messages. |
10 | | | class WorkerResponse { |
11 | | | /// [WorkerResponse] with a valid [result]. |
12 | | 1 | WorkerResponse(this._result) |
13 | | | : _error = null, |
14 | | | _eos = false; |
15 | | |
|
16 | | | /// [WorkerResponse] with an error message and an optional (string) [StackTrace]. |
17 | | 2 | WorkerResponse.withError(Object exception, [StackTrace? stackTrace]) |
18 | | 2 | : _error = SquadronException.from( |
19 | | 2 | error: exception, stackTrace: stackTrace ?? StackTrace.current), |
20 | | | _result = null, |
21 | | | _eos = false; |
22 | | |
|
23 | | | /// Special [WorkerResponse] message to indicate the end of a stream. |
24 | | 1 | const WorkerResponse._endOfStream() |
25 | | | : _result = null, |
26 | | | _error = null, |
27 | | | _eos = true; |
28 | | |
|
29 | | | /// End of stream response. |
30 | | | static const closeStream = WorkerResponse._endOfStream(); |
31 | | |
|
32 | | | static const _$result = 'a'; |
33 | | | static const _$error = 'b'; |
34 | | | static const _$eos = 'c'; |
35 | | |
|
36 | | | /// Creates a new [WorkerResponse] from a message sent by the worker. |
37 | | 2 | WorkerResponse.deserialize(Map message) |
38 | | 2 | : _result = message[_$result], |
39 | | 3 | _error = SquadronException.deserialize(message[_$error]), |
40 | | 2 | _eos = message[_$eos] ?? false; |
41 | | |
|
42 | | | /// [WorkerResponse] serialization. |
43 | | 2 | Map<String, dynamic> serialize() { |
44 | | 2 | if (_error != null) { |
45 | | 4 | return {_$error: _error!.serialize()}; |
46 | | 2 | } else if (_eos) { |
47 | | 1 | return const {_$eos: true}; |
48 | | 2 | } else if (_result == null) { |
49 | | 0 | return const {}; |
50 | | | } else { |
51 | | 3 | return {_$result: _result}; |
52 | | | } |
53 | | 1 | } |
54 | | |
|
55 | | | /// Flag indicating the end of the [Stream]ing operation. |
56 | | 3 | bool get endOfStream => _eos; |
57 | | | final bool _eos; |
58 | | |
|
59 | | | /// The [WorkerResponse] exception, if any. |
60 | | 3 | SquadronException? get error => _error; |
61 | | | final SquadronException? _error; |
62 | | |
|
63 | | | /// Flag indicating whether an error occured. |
64 | | 3 | bool get hasError => _error != null; |
65 | | |
|
66 | | | /// Retrieves the result associated to this [WorkerResponse]. If the [WorkerResponse] contains an error, |
67 | | | /// an the [error] exception is thrown. |
68 | | 5 | dynamic get result => hasError ? throw error! : _result; |
69 | | | final dynamic _result; |
70 | | | } |