Line data Source code
1 : import 'package:get/get.dart'; 2 : import 'rx_interface.dart'; 3 : import 'utils/debouncer.dart'; 4 : 5 1 : void ever(RxInterface listener, Function(dynamic) callback, 6 4 : {bool condition = true}) { 7 2 : listener.subject.stream.listen((event) { 8 : if (condition) { 9 : callback(event.$new); 10 : } 11 1 : }); 12 : } 13 4 : 14 1 : void once(RxInterface listener, Function(dynamic) callback, 15 1 : {bool condition = true}) { 16 2 : int times = 0; 17 : listener.subject.stream.listen((event) { 18 : if (!condition) return null; 19 : times++; 20 : if (times < 2) { 21 1 : callback(event.$new); 22 : } 23 : }); 24 4 : } 25 : 26 : void interval(RxInterface listener, Function(dynamic) callback, 27 2 : {Duration time, bool condition = true}) { 28 : bool debounceActive = false; 29 2 : listener.subject.stream.listen((event) async { 30 : if (debounceActive || !condition) return null; 31 : debounceActive = true; 32 : await Future.delayed(time ?? Duration(seconds: 1)); 33 1 : debounceActive = false; 34 : callback(event.$new); 35 1 : }); 36 4 : } 37 2 : 38 2 : void debounce(RxInterface listener, Function(dynamic) callback, 39 : {Duration time}) { 40 : final _debouncer = Debouncer(delay: time ?? Duration(milliseconds: 800)); 41 : listener.subject.stream.listen((event) { 42 : _debouncer(() { 43 : callback(event.$new); 44 : }); 45 : }); 46 : }