newPoller<T> static method
Creates a Stream that polls a callback
at a specified interval
.
Implementation
static Stream<T> newPoller<T>(
Future<T> Function() callback,
Duration interval,
) {
final controller = StreamController<T>.broadcast();
var started = false;
void startPolling() async {
started = true;
try {
while (!controller.isClosed) {
try {
final result = await callback();
controller.add(result);
} catch (e) {
controller.addError(e);
}
await Future<void>.delayed(interval);
}
} catch (e) {
if (!controller.isClosed) {
controller.addError(e);
controller.close();
}
}
}
controller.onListen = () {
if (!started) {
startPolling();
}
};
return controller.stream;
}