1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import '../../channel.dart'; |
4 | | | import '../../exceptions/squadron_error.dart'; |
5 | | | import '../../exceptions/squadron_exception.dart'; |
6 | | | import '../../worker/worker_request.dart'; |
7 | | | import '../../worker/worker_response.dart'; |
8 | | | import '_forward_stream_controller.dart'; |
9 | | |
|
10 | | | class ResultStream { |
11 | | 18 | ResultStream( |
12 | | | Channel channel, |
13 | | | WorkerRequest req, |
14 | | | Stream<WorkerResponse> Function() sendRequest, |
15 | | | bool streaming, |
16 | | | ) { |
17 | | 5 | final streamIdCompleter = streaming ? Completer<int?>() : null; |
18 | | 18 | final command = req.command, token = req.cancelToken; |
19 | | |
|
20 | | 15 | void $decodeStreamOfResponses(WorkerResponse res) { |
21 | | 11 | if (!res.unwrapInPlace(channel)) return; |
22 | | |
|
23 | | 11 | final hasStreamId = streamIdCompleter!.isCompleted; |
24 | | 5 | if (res.endOfStream) { |
25 | | | // handle endofStream |
26 | | 4 | if (!hasStreamId) { |
27 | | 0 | streamIdCompleter.complete(null); |
28 | | 0 | channel.logger?.e('Invalid state: endOfStream before streamId'); |
29 | | 0 | _controller.addError( |
30 | | 0 | SquadronErrorExt.create('Invalid state: unexpected endOfStream')); |
31 | | | } |
32 | | 12 | _controller.close(); |
33 | | 4 | return; |
34 | | | } |
35 | | |
|
36 | | 5 | final error = res.error; |
37 | | 6 | if (error == null && !hasStreamId) { |
38 | | | // the first result from a streaming operation is the stream ID |
39 | | 20 | streamIdCompleter.complete((res.result as num).toInt()); |
40 | | 6 | } else if (error != null) { |
41 | | 10 | _controller.addError(error); |
42 | | 4 | if (!hasStreamId) { |
43 | | | // if any error comes before the stream ID, somethind bad happened |
44 | | 0 | streamIdCompleter.complete(null); |
45 | | 0 | _controller.close(); |
46 | | 0 | return; |
47 | | | } |
48 | | | } else { |
49 | | 0 | try { |
50 | | 21 | _controller.add(res.result); |
51 | | 0 | } catch (ex, st) { |
52 | | 0 | _controller.addError(SquadronException.from(ex, st, command)); |
53 | | | } |
54 | | | } |
55 | | |
|
56 | | 8 | final canceled = token?.exception; |
57 | | 6 | if (canceled != null) { |
58 | | 0 | _controller.addError(canceled); |
59 | | 0 | _controller.close(); |
60 | | | } |
61 | | 6 | } |
62 | | |
|
63 | | 17 | void $decodeSingleResponse(WorkerResponse res) { |
64 | | 16 | if (!res.unwrapInPlace(channel)) return; |
65 | | |
|
66 | | 8 | final error = res.error; |
67 | | 8 | if (error != null) { |
68 | | 10 | _controller.addError(error); |
69 | | | } else { |
70 | | 0 | try { |
71 | | 32 | _controller.add(res.result); |
72 | | 0 | } catch (ex, st) { |
73 | | 0 | _controller.addError(SquadronException.from(ex, st, command)); |
74 | | | } |
75 | | | } |
76 | | |
|
77 | | 24 | _controller.close(); |
78 | | 8 | } |
79 | | |
|
80 | | 12 | Future<int?> $getStreamId(StreamSubscription sub) async { |
81 | | 3 | streamIdCompleter as Completer<int?>; |
82 | | 3 | var count = 0; |
83 | | 7 | if (sub.isPaused && !streamIdCompleter.isCompleted) { |
84 | | | // if the subscription was paused and the streamId is not available, |
85 | | | // resume to have the streamId eventually come through. |
86 | | 0 | while (sub.isPaused) { |
87 | | 0 | count++; |
88 | | 0 | sub.resume(); |
89 | | | } |
90 | | | } |
91 | | | // wait for the streamId... |
92 | | 6 | final streamId = await streamIdCompleter.future; |
93 | | | // restore subscription pause |
94 | | 3 | while (count > 0) { |
95 | | 0 | count--; |
96 | | 0 | sub.pause(); |
97 | | | } |
98 | | 3 | return streamId; |
99 | | 3 | } |
100 | | |
|
101 | | 18 | Future<void> $onCancel() async { |
102 | | 27 | final sub = _controller.subscription; |
103 | | 9 | if (streamIdCompleter != null && sub != null) { |
104 | | | // this is a streaming operation and the subscription is active, so |
105 | | | // we need to inform the worker that the stream has been canceled |
106 | | 6 | final streamId = await $getStreamId(sub); |
107 | | | if (streamId != null) { |
108 | | 6 | channel.cancelStream(streamId); |
109 | | | } |
110 | | 6 | await sub.cancel(); |
111 | | | } |
112 | | 9 | } |
113 | | |
|
114 | | 10 | void $closeWithError(Object error, StackTrace? st) { |
115 | | 5 | _controller.addError(SquadronException.from(error, st, command)); |
116 | | 4 | _controller.close(); |
117 | | 2 | } |
118 | | |
|
119 | | 18 | void $onListen() { |
120 | | 9 | try { |
121 | | | // do not send the request if the token is already canceled |
122 | | 11 | token?.throwIfCanceled(); |
123 | | | // send the request and start decoding responses |
124 | | 45 | _controller.attachSubscription(sendRequest().listen( |
125 | | 9 | streaming ? $decodeStreamOfResponses : $decodeSingleResponse, |
126 | | 9 | onError: $closeWithError, |
127 | | 27 | onDone: _controller.close, |
128 | | | cancelOnError: false, |
129 | | | )); |
130 | | 0 | } catch (ex, st) { |
131 | | 0 | $closeWithError(ex, st); |
132 | | | } |
133 | | 9 | } |
134 | | |
|
135 | | 23 | _controller = ForwardStreamController<dynamic>( |
136 | | | onListen: $onListen, |
137 | | | onCancel: $onCancel, |
138 | | | ); |
139 | | 9 | } |
140 | | |
|
141 | | 10 | late final ForwardStreamController<dynamic> _controller; |
142 | | |
|
143 | | 27 | Stream<dynamic> get stream => _controller.stream; |
144 | | |
|
145 | | 0 | Future<void> get done => _controller.done; |
146 | | | } |