rx_set.dart
7.23 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'rx_impl.dart';
import 'rx_interface.dart';
import 'rx_typedefs.dart';
class RxSet<E> implements Set<E>, RxInterface<Set<E>> {
RxSet([Set<E> initial]) {
_list = initial;
}
RxSet<E> _list = 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();
Map<Stream<Set<E>>, StreamSubscription> _subscriptions = Map();
/// Adds [item] only if [condition] resolves to true.
void addIf(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(condition, Iterable<E> items) {
if (condition is Condition) condition = condition();
if (condition is bool && condition) addAll(items);
}
operator []=(int index, E val) {
_list[index] = val;
subject.add(_list);
}
/// Special override to push() element(s) in a reactive way
/// inside the List,
RxSet<E> operator +(Iterable<E> val) {
addAll(val);
subject.add(_list);
return this;
}
@override
bool add(E value) {
final val = _list.add(value);
subject.add(_list);
return val;
}
void addAll(Iterable<E> item) {
_list.addAll(item);
subject.add(_list);
}
/// 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);
}
void insert(int index, E item) {
_list.insert(index, item);
subject.add(_list);
}
void insertAll(int index, Iterable<E> iterable) {
_list.insertAll(index, iterable);
subject.add(_list);
}
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) {
bool hasRemoved = _list.remove(item);
if (hasRemoved) {
subject.add(_list);
}
return hasRemoved;
}
E removeAt(int index) {
E item = _list.removeAt(index);
subject.add(_list);
return item;
}
E removeLast() {
E item = _list.removeLast();
subject.add(_list);
return item;
}
void removeRange(int start, int end) {
_list.removeRange(start, end);
subject.add(_list);
}
void removeWhere(bool Function(E) test) {
_list.removeWhere(test);
subject.add(_list);
}
void clear() {
_list.clear();
subject.add(_list);
}
void sort([int compare(E a, E b)]) {
_list.sort();
subject.add(_list);
}
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);
subject.add(_list);
}
/// 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 _list;
}
String get string => value.toString();
addListener(Stream<Set<E>> rxGetx) {
if (_subscriptions.containsKey(rxGetx)) {
return;
}
_subscriptions[rxGetx] = rxGetx.listen((data) {
subject.add(data);
});
}
set value(Iterable<E> val) {
if (_list == val) return;
_list = val;
subject.add(_list);
}
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);
void bindStream(Stream<Iterable<E>> stream) =>
stream.listen((va) => value = va);
@override
E get first => _list.first;
@override
E get last => _list.last;
@override
bool any(bool Function(E) test) {
return _list.any(test);
}
@override
Set<R> cast<R>() {
return _list.cast<R>();
}
@override
bool contains(Object element) {
return _list.contains(element);
}
@override
E elementAt(int index) {
return _list.elementAt(index);
}
@override
bool every(bool Function(E) test) {
return _list.every(test);
}
@override
Iterable<T> expand<T>(Iterable<T> Function(E) f) {
return _list.expand(f);
}
@override
E firstWhere(bool Function(E) test, {E Function() orElse}) {
return _list.firstWhere(test, orElse: orElse);
}
@override
T fold<T>(T initialValue, T Function(T, E) combine) {
return _list.fold(initialValue, combine);
}
@override
Iterable<E> followedBy(Iterable<E> other) {
return _list.followedBy(other);
}
@override
void forEach(void Function(E) f) {
_list.forEach(f);
}
@override
String join([String separator = ""]) {
return _list.join(separator);
}
@override
E lastWhere(bool Function(E) test, {E Function() orElse}) {
return _list.lastWhere(test, orElse: orElse);
}
@override
Iterable<T> map<T>(T Function(E) f) {
return _list.map(f);
}
@override
E reduce(E Function(E, E) combine) {
return _list.reduce(combine);
}
@override
E get single => _list.single;
@override
E singleWhere(bool Function(E) test, {E Function() orElse}) {
return _list.singleWhere(test, orElse: orElse);
}
@override
Iterable<E> skip(int count) {
return _list.skip(count);
}
@override
Iterable<E> skipWhile(bool Function(E) test) {
return _list.skipWhile(test);
}
@override
Iterable<E> take(int count) {
return _list.take(count);
}
@override
Iterable<E> takeWhile(bool Function(E) test) {
return _list.takeWhile(test);
}
@override
List<E> toList({bool growable = true}) {
return _list.toList(growable: growable);
}
@override
Set<E> toSet() {
return _list.toSet();
}
@override
Iterable<E> where(bool Function(E) test) {
return _list.where(test);
}
@override
Iterable<T> whereType<T>() {
return _list.whereType<T>();
}
@override
bool containsAll(Iterable<Object> other) {
return _list.containsAll(other);
}
@override
Set<E> difference(Set<Object> other) {
return _list.difference(other);
}
@override
Set<E> intersection(Set<Object> other) {
return _list.intersection(other);
}
@override
E lookup(Object object) {
return _list.lookup(object);
}
@override
void removeAll(Iterable<Object> elements) {
_list.removeAll(elements);
}
@override
void retainAll(Iterable<Object> elements) {
_list.retainAll(elements);
}
@override
void retainWhere(bool Function(E) E) {
_list.retainWhere(E);
}
@override
Set<E> union(Set<E> other) {
return _list.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);
}
}