-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.dart
206 lines (174 loc) · 7.73 KB
/
utils.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
import 'dart:async';
import 'package:dartlcemodel/dartlcemodel_lce.dart';
import 'package:dartlcemodel/dartlcemodel_model.dart';
/// [LceState] stream extensions
extension LceStreamExtension<DATA extends Object> on Stream<LceState<DATA>> {
/// Emits error event when state is [LceError] and [predicate] returns true
Stream<LceState<DATA>> errorIf(bool Function(LceError<DATA> error) predicate) => map((state) {
if (state is LceError<DATA> && predicate(state)) {
throw state.error;
} else {
return state;
}
});
/// Emits error on every [LceError]
Stream<LceState<DATA>> errorOnError() => errorIf((_) => true);
/// Emits error on every [LceError] with empty data
Stream<LceState<DATA>> errorOnEmptyError() => errorIf((error) => null == error.data);
/// Returns model's data stream dropping state information
/// [DATA] Source model data type
/// [errorPredicate] A predicate to check error state. If predicate returns true, the stream
/// emits error event
Stream<DATA> getData([bool Function(LceError<DATA> error)? errorPredicate]) => errorIf(errorPredicate ?? (_) => true)
.where((state) => null != state.data)
.map((state) => state.data!)
.distinct();
/// Model's _valid_ data stream with state information dropped.
/// [DATA] Source model data type
/// [errorPredicate] A predicate to check error state. If predicate returns true, the stream
/// emits error event
Stream<DATA> validData([bool Function(LceError<DATA> error)? errorPredicate]) => errorIf(errorPredicate ?? (_) => true)
.where((state) => null != state.data && state.dataIsValid)
.map((state) => state.data!)
.distinct();
/// Model's data stream with state information dropped.
/// No error events
/// [DATA] Source model data type
Stream<DATA> dataNoErrors() => getData((_) => false);
/// Model's data stream with state information dropped.
/// Emits error event on upstream error state
/// [DATA] Source model data type
Stream<DATA> dataWithErrors() => getData((_) => true);
/// Model's data stream with state information dropped.
/// Emits error event on upstream error state when upstream has no data
/// [DATA] Source model data type
Stream<DATA> dataWithEmptyErrors() => getData((state) => null == state.data);
/// Maps each [DATA] to future for [DATA_2] and merges back to LceState.
/// If error occurs in [mapper] emits [LceError].
/// Example: load some [DATA_2] from server using original [DATA] as a parameter.
/// [DATA_2] Resulting data type
/// mapper Data mapper
Stream<LceState<DATA_2>> flatMapSingleData<DATA_2 extends Object>(Future<DATA_2> Function(DATA data) mapper) =>
asyncMap((state) async {
final data1 = state.data;
if (null == data1) {
return LceState<DATA_2>.loading(null, false);
} else {
try {
return state.combine(LceState.content(await mapper(data1), true), (_, data2) => data2);
} on Exception catch (e) {
return LceState.error(null, false, e);
} catch (e) {
return LceState.error(null, false, Exception(e.toString()));
}
}
});
/// Substitutes [LceLoading] with empty data with state produced by [block]
/// [block] transformation block
Stream<LceState<DATA>> onEmptyLoadingReturn(LceState<DATA> Function(LceLoading<DATA> loading) block) => map((state) =>
state.whenElse(
loading: (loading) => null == loading.data ? block(loading) : loading,
onElse: (state) => state
)
);
/// Substitutes [LceLoading] empty data with data produced by [block]
/// [block] Item creation block
Stream<LceState<DATA>> onEmptyLoadingReturnData(DATA Function() block) => map((state) =>
state.whenElse(
loading: (loading) => null == loading.data ? LceState.loading(block(), loading.dataIsValid, loading.type) : loading,
onElse: (state) => state
)
);
/// Maps an upstream error to LceError
/// [errorData] Evaluates data for error state
Stream<LceState<DATA>> errorToLce([DATA? Function(Object error)? errorData]) => transform(
StreamTransformer.fromHandlers(
handleError: (error, stacktrace, sink) {
sink.add(
LceState.error(
null != errorData ? errorData(error) : null,
false,
error is Exception ? error : Exception(error.toString())
)
);
}
)
);
}
/// [LceState] stream extensions
extension LceUseCaseExtension<DATA extends Object> on LceUseCase<DATA> {
/// Creates a use-case wrapper that converts [DATA_1] to [DATA_2]
/// [DATA_2] Resulting data type
/// [mapper] Data mapper
LceUseCase<DATA_2> map<DATA_2 extends Object>(DATA_2 Function(DATA data) mapper) => _LceUseCaseMapper<DATA, DATA_2>(this, mapper);
/// Takes the [LceUseCase.state] of model that is being refreshed each time [refresh] emits a value
/// Useful when you create a model as a result of mapping of some input (params for example) and the
/// [LceUseCase.refresh] property becomes invisible for the outside world
/// [DATA] Source model data type
/// [refresh] Whenever this stream emits a value, the model is refreshed
Stream<LceState<DATA>> withRefresh(Stream<dynamic> refresh) {
final controller = StreamController<LceState<DATA>>(sync: true);
late StreamSubscription<LceState<DATA>> stateSubscription;
late StreamSubscription<dynamic> refreshSubscription;
controller.onListen = () {
stateSubscription = state.listen(controller.add, onError: controller.addError, onDone: controller.close);
refreshSubscription = refresh.asyncMap((event) async => await this.refresh()).listen((_) { }, onError: (_) { });
};
controller.onPause = () {
stateSubscription.pause();
refreshSubscription.pause();
};
controller.onResume = () {
stateSubscription.resume();
refreshSubscription.resume();
};
controller.onCancel = () {
stateSubscription.cancel();
refreshSubscription.cancel();
};
return controller.stream;
}
/// Wraps use-case to refresh on each subscription
LceUseCase<DATA> refreshed() => _RefreshedLceUse(this);
}
/// Use-case mapper
class _LceUseCaseMapper<DATA extends Object, DATA_2 extends Object> implements LceUseCase<DATA_2> {
final LceUseCase<DATA> _wrapped;
final DATA_2 Function(DATA data) _mapper;
const _LceUseCaseMapper(this._wrapped, this._mapper);
@override
Stream<LceState<DATA_2>> get state => _wrapped.state.map((state) => state.map(_mapper));
@override
Future<void> refresh() => _wrapped.refresh();
}
/// Use-case mapper
class _RefreshedLceUse<DATA extends Object> implements LceUseCase<DATA> {
final LceUseCase<DATA> _wrapped;
const _RefreshedLceUse(this._wrapped);
@override
Stream<LceState<DATA>> get state async* {
await _wrapped.refresh();
yield* _wrapped.state;
}
@override
Future<void> refresh() => _wrapped.refresh();
}
/// [LceState] stream extensions
extension LceModelExtension<DATA extends Object, PARAMS extends Object> on LceModel<DATA, PARAMS> {
/// Creates a model wrapper that converts [DATA_1] to [DATA_2]
/// [DATA_2] Resulting data type
/// [mapper] Data mapper
LceModel<DATA_2, PARAMS> map<DATA_2 extends Object>(DATA_2 Function(DATA data) mapper) => _LceModelMapper<DATA, DATA_2, PARAMS>(this, mapper);
}
/// Model mapper
class _LceModelMapper<DATA extends Object, DATA_2 extends Object, PARAMS extends Object> implements LceModel<DATA_2, PARAMS> {
final LceModel<DATA, PARAMS> _wrapped;
final DATA_2 Function(DATA data) _mapper;
const _LceModelMapper(this._wrapped, this._mapper);
@override
PARAMS get params => _wrapped.params;
@override
Stream<LceState<DATA_2>> get state => _wrapped.state.map((state) => state.map(_mapper));
@override
Future<void> refresh() => _wrapped.refresh();
}