Created
March 5, 2021 18:24
-
-
Save xsahil03x/e3d5ac91f45cb8b610ca669832db634c to your computer and use it in GitHub Desktop.
RxDart based implementation of rate limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:rxdart/rxdart.dart'; | |
class StreamDebounce { | |
final Function func; | |
final Duration interval; | |
final subject = PublishSubject<List<dynamic>>(); | |
StreamSubscription subscriber; | |
StreamDebounce(this.func, this.interval) { | |
subscriber = subject.stream | |
.throttleTime(interval, trailing: true, leading: true) | |
.listen((event) { | |
Function.apply(func, event); | |
}); | |
} | |
void clean() { | |
subscriber?.cancel(); | |
subject.close(); | |
} | |
void call(List<dynamic> args) { | |
subject.sink.add(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment