Last active
January 14, 2019 08:33
-
-
Save amsokol/897655e2296fe593ff3779fe669906c6 to your computer and use it in GitHub Desktop.
flutter-grpc-tutorial.flutter_client.lib.bandwidth_buffer.dart
This file contains hidden or 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'; | |
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