Skip to content

Instantly share code, notes, and snippets.

@xsahil03x
Created March 5, 2021 18:24
Show Gist options
  • Save xsahil03x/e3d5ac91f45cb8b610ca669832db634c to your computer and use it in GitHub Desktop.
Save xsahil03x/e3d5ac91f45cb8b610ca669832db634c to your computer and use it in GitHub Desktop.
RxDart based implementation of rate limit
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