rx_set.dart
6.95 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import 'dart:async';
import 'dart:collection';
import 'package:flutter/foundation.dart';
import '../rx_core/rx_impl.dart';
import '../rx_core/rx_interface.dart';
import '../rx_typedefs/rx_typedefs.dart';
class RxSet<E> implements Set<E>, RxInterface<Set<E>> {
RxSet([Set<E> initial]) {
if (initial != null) _set = initial;
}
Set<E> _set = <E>{};
@override
Iterator<E> get iterator => value.iterator;
@override
bool get isEmpty => value.isEmpty;
bool get canUpdate {
return _subscriptions.length > 0;
}
@override
bool get isNotEmpty => value.isNotEmpty;
StreamController<Set<E>> subject = StreamController<Set<E>>.broadcast();
final _subscriptions = HashMap<Stream<Set<E>>, StreamSubscription>();
/// Adds [item] only if [condition] resolves to true.
void addIf(dynamic condition, E item) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) add(item);
}
/// Adds all [items] only if [condition] resolves to true.
void addAllIf(dynamic condition, Iterable<E> items) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) addAll(items);
}
void refresh() {
subject.add(_set);
}
/// Special override to push() element(s) in a reactive way
/// inside the List,
RxSet<E> operator +(Set<E> val) {
addAll(val);
refresh();
return this;
}
@override
bool add(E value) {
final val = _set.add(value);
refresh();
return val;
}
@override
void addAll(Iterable<E> item) {
_set.addAll(item);
refresh();
}
/// Adds only if [item] is not null.
void addNonNull(E item) {
if (item != null) add(item);
}
/// Adds only if [item] is not null.
void addAllNonNull(Iterable<E> item) {
if (item != null) addAll(item);
}
int get length => value.length;
/// Removes an item from the list.
///
/// This is O(N) in the number of items in the list.
///
/// Returns whether the item was present in the list.
bool remove(Object item) {
var hasRemoved = _set.remove(item);
if (hasRemoved) {
refresh();
}
return hasRemoved;
}
void removeWhere(bool Function(E) test) {
_set.removeWhere(test);
refresh();
}
void clear() {
_set.clear();
refresh();
}
void close() {
_subscriptions.forEach((observable, subscription) {
subscription.cancel();
});
_subscriptions.clear();
subject.close();
}
/// Replaces all existing items of this list with [item]
void assign(E item) {
clear();
add(item);
}
void update(void fn(Iterable<E> value)) {
fn(value);
refresh();
}
/// Replaces all existing items of this list with [items]
void assignAll(Iterable<E> items) {
clear();
addAll(items);
}
@protected
Set<E> get value {
if (getObs != null) {
getObs.addListener(subject.stream);
}
return _set;
}
String get string => value.toString();
void addListener(Stream<Set<E>> rxGetX) {
if (_subscriptions.containsKey(rxGetX)) {
return;
}
_subscriptions[rxGetX] = rxGetX.listen((data) {
subject.add(data);
});
}
set value(Set<E> val) {
if (_set == val) return;
_set = val;
refresh();
}
Stream<Set<E>> get stream => subject.stream;
StreamSubscription<Set<E>> listen(void Function(Set<E>) onData,
{Function onError, void Function() onDone, bool cancelOnError}) =>
stream.listen(onData, onError: onError, onDone: onDone);
/// Binds an existing [Stream<Set>] to this [RxSet].
/// You can bind multiple sources to update the value.
/// Closing the subscription will happen automatically when the observer
/// Widget ([GetX] or [Obx]) gets unmounted from the Widget tree.
void bindStream(Stream<Set<E>> stream) {
_subscriptions[stream] = stream.listen((va) => value = va);
}
@override
E get first => value.first;
@override
E get last => value.last;
@override
bool any(bool Function(E) test) {
return value.any(test);
}
@override
Set<R> cast<R>() {
return value.cast<R>();
}
@override
bool contains(Object element) {
return value.contains(element);
}
@override
E elementAt(int index) {
return value.elementAt(index);
}
@override
bool every(bool Function(E) test) {
return value.every(test);
}
@override
Iterable<T> expand<T>(Iterable<T> Function(E) f) {
return value.expand(f);
}
@override
E firstWhere(bool Function(E) test, {E Function() orElse}) {
return value.firstWhere(test, orElse: orElse);
}
@override
T fold<T>(T initialValue, T Function(T, E) combine) {
return value.fold(initialValue, combine);
}
@override
Iterable<E> followedBy(Iterable<E> other) {
return value.followedBy(other);
}
@override
void forEach(void Function(E) f) {
value.forEach(f);
}
@override
String join([String separator = ""]) {
return value.join(separator);
}
@override
E lastWhere(bool Function(E) test, {E Function() orElse}) {
return value.lastWhere(test, orElse: orElse);
}
@override
Iterable<T> map<T>(T Function(E) f) {
return value.map(f);
}
@override
E reduce(E Function(E, E) combine) {
return value.reduce(combine);
}
@override
E get single => value.single;
@override
E singleWhere(bool Function(E) test, {E Function() orElse}) {
return value.singleWhere(test, orElse: orElse);
}
@override
Iterable<E> skip(int count) {
return value.skip(count);
}
@override
Iterable<E> skipWhile(bool Function(E) test) {
return value.skipWhile(test);
}
@override
Iterable<E> take(int count) {
return value.take(count);
}
@override
Iterable<E> takeWhile(bool Function(E) test) {
return value.takeWhile(test);
}
@override
List<E> toList({bool growable = true}) {
return value.toList(growable: growable);
}
@override
Set<E> toSet() {
return value.toSet();
}
@override
Iterable<E> where(bool Function(E) test) {
return value.where(test);
}
@override
Iterable<T> whereType<T>() {
return value.whereType<T>();
}
@override
bool containsAll(Iterable<Object> other) {
return value.containsAll(other);
}
@override
Set<E> difference(Set<Object> other) {
return value.difference(other);
}
@override
Set<E> intersection(Set<Object> other) {
return value.intersection(other);
}
@override
E lookup(Object object) {
return value.lookup(object);
}
@override
void removeAll(Iterable<Object> elements) {
_set.removeAll(elements);
refresh();
}
@override
void retainAll(Iterable<Object> elements) {
_set.retainAll(elements);
refresh();
}
@override
void retainWhere(bool Function(E) E) {
_set.retainWhere(E);
refresh();
}
@override
Set<E> union(Set<E> other) {
return value.union(other);
}
}
extension SetExtension<E> on Set<E> {
RxSet<E> get obs {
if (this != null) {
return RxSet<E>(<E>{})..addAllNonNull(this);
} else {
return RxSet<E>(null);
}
}
}