This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.dart
211 lines (178 loc) · 5.63 KB
/
watcher.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import 'dart:async';
import 'package:codenic_bloc_use_case/src/base.dart';
import 'package:codenic_bloc_use_case/src/util/ensure_async.dart';
import 'package:equatable/equatable.dart';
import 'package:fpdart/fpdart.dart';
import 'package:meta/meta.dart';
part 'verbose_stream.dart';
part 'watcher_state.dart';
/// {@template Watcher}
///
/// An abstract use case for running a stream asynchronously via a cubit which
/// accepts a [P] parameter for creating the stream.
///
/// The stream is created from the obtained [VerboseStream][L][R] when [watch]
/// is executed successfully. [L] is the error returned when stream creation
/// fails.
///
/// The created stream emits either an [L] error event or an [R] data event.
///
/// {@endtemplate}
abstract class Watcher<P, L, R> extends DistinctCubit<WatcherState>
with BaseUseCase<P, L, VerboseStream<L, R>> {
/// {@macro Watcher}
Watcher() : super(const WatcherInitial(DistinctCubit.initialActionToken));
StreamSubscription<R>? _streamSubscription;
/// The latest value emitted by calling [watch] which can either reference
/// the [leftValue] or the [rightValue].
///
/// This can be used to determine which is latest among the two values.
///
/// If [watch] has not been called even once, then this is `null`.
@override
Either<L, VerboseStream<L, R>>? get value => super.value;
/// {@template Watcher.leftValue}
///
/// The error emitted by the [watch] call that prevented the creation of a
/// stream.
///
/// If [watch] call has not failed even once, then this is `null`.
///
/// {@endtemplate}
@override
L? get leftValue => super.leftValue;
/// {@template Watcher.rightValue}
///
/// The [VerboseStream] returned by a successful [watch] call.
///
/// If [watch] call has not succeeded even once, then this is `null`.
///
/// {@endtemplate}
@override
VerboseStream<L, R>? get rightValue => super.rightValue;
/// {@template Watcher.event}
///
/// The latest value emitted by the [watch]-created stream which can either
/// reference the [leftEvent] or the [rightEvent].
///
/// This can be used to determine which is latest among the two values.
///
/// If [watch]-created stream has not emitted an event even once, then this
/// is `null`.
///
/// {@endtemplate}
Either<L, R>? _event;
/// {@macro Watcher.event}
Either<L, R>? get event => _event;
@protected
set event(Either<L, R>? newEvent) {
_event = newEvent;
newEvent?.fold((l) => _leftEvent = l, (r) => _rightEvent = r);
}
/// {@template Watcher.leftEvent}
///
/// The last error event emitted by the [watch]-created stream.
///
/// If any of the [watch]-created stream has not emitted an error event even
/// once, then this is `null`.
///
/// {@endtemplate}
L? _leftEvent;
/// {@macro Watcher.errorEvent}
L? get leftEvent => _leftEvent;
/// {@template Watcher.rightEvent}
///
/// The last data event emitted by the [watch]-created stream.
///
/// If any of the [watch]-created stream has not emitted a data event even
/// once, then this is `null`.
///
/// {@endtemplate}
R? _rightEvent;
/// {@macro Watcher.rightEvent}
R? get rightEvent => _rightEvent;
@override
Future<void> close() {
_streamSubscription?.cancel();
return super.close();
}
/// Starts creating the stream to watch.
///
/// This will initially emit a [StartWatching] state followed either by a
/// [StartWatchFailed] or [StartWatchSuccess].
///
/// Afterwards, the generated stream may emit a [WatchDataReceived] for data
/// events, a [WatchErrorReceived] for error events, or a [WatchDone] when
/// the stream has been closed.
Future<void> watch({required P params, bool cancelOnError = false}) async {
final actionToken = requestNewActionToken();
await (_streamSubscription?.cancel() ?? ensureAsync());
if (isClosed) return;
if (distinctEmit(
actionToken,
() => StartWatching(actionToken),
) ==
null) {
return;
}
final result = await onCall(params);
if (isClosed) return;
distinctEmit(actionToken, () {
setParamsAndValue(params, result);
return result.fold(
(l) => StartWatchFailed<L>(l, actionToken),
(r) {
_streamSubscription = r.listen(
(data) {
if (isClosed) return;
distinctEmit(
actionToken,
() {
event = Right(data);
return WatchDataReceived<R>(data, actionToken);
},
);
},
onError: (error) {
if (isClosed) return;
distinctEmit(
actionToken,
() {
event = Left(error);
return WatchErrorReceived<L>(error, actionToken);
},
);
},
onDone: () {
if (isClosed) return;
distinctEmit(
actionToken,
() => WatchDone(actionToken),
);
},
cancelOnError: cancelOnError,
);
return StartWatchSuccess(r, actionToken);
},
);
});
}
/// Clears all the data then emits a [WatcherInitial].
@override
Future<void> reset() async {
final actionToken = requestNewActionToken();
await (_streamSubscription?.cancel() ?? ensureAsync());
if (isClosed) return;
distinctEmit(
actionToken,
() {
super.reset();
_event = null;
_leftEvent = null;
_rightEvent = null;
_streamSubscription = null;
return WatcherInitial(actionToken);
},
);
}
}