Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amsokol/897655e2296fe593ff3779fe669906c6 to your computer and use it in GitHub Desktop.
Save amsokol/897655e2296fe593ff3779fe669906c6 to your computer and use it in GitHub Desktop.
flutter-grpc-tutorial.flutter_client.lib.bandwidth_buffer.dart
import 'dart:async';
class BandwidthBuffer<T> {
final Duration duration;
final void Function(List<T>) onReceive;
List<T> _list = <T>[];
Timer _timer;
BandwidthBuffer({this.duration, this.onReceive});
void start() {
_timer = Timer.periodic(duration, _onTimeToSend);
}
void stop() {
if (_timer != null) {
_timer.cancel();
_timer = null;
}
}
void send(T t) {
_list.add(t);
}
void _onTimeToSend(Timer t) {
if (_list.length > 0 && onReceive != null) {
var list = _list;
_list = <T>[];
onReceive(list);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment