rx_event.dart
1.16 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
import 'package:get/get.dart';
import 'rx_interface.dart';
import 'utils/debouncer.dart';
void ever(RxInterface listener, Function(dynamic) callback,
{bool condition = true}) {
listener.subject.stream.listen((event) {
if (condition) {
callback(event);
}
});
}
void once(RxInterface listener, Function(dynamic) callback,
{bool condition = true}) {
int times = 0;
listener.subject.stream.listen((event) {
if (!condition) return null;
times++;
if (times < 2) {
callback(event);
}
});
}
void interval(RxInterface listener, Function(dynamic) callback,
{Duration time, bool condition = true}) {
bool debounceActive = false;
listener.subject.stream.listen((event) async {
if (debounceActive || !condition) return null;
debounceActive = true;
await Future.delayed(time ?? Duration(seconds: 1));
debounceActive = false;
callback(event);
});
}
void debounce(RxInterface listener, Function(dynamic) callback,
{Duration time}) {
final _debouncer = Debouncer(delay: time ?? Duration(milliseconds: 800));
listener.subject.stream.listen((event) {
_debouncer(() {
callback(event);
});
});
}