Skip to content

Instantly share code, notes, and snippets.

@natebosch
Last active August 29, 2015 14:22
Show Gist options
  • Save natebosch/f9eb8c63e7ece4664b4c to your computer and use it in GitHub Desktop.
Save natebosch/f9eb8c63e7ece4664b4c to your computer and use it in GitHub Desktop.

Proof of concept for holding off a stream.listen callback until a trigger Future completes.

import 'dart:async';
void main() {
Future trigger = new Future.delayed(new Duration(seconds: 2));
Stream stream = new Stream.fromIterable([1,2,3]);
stream.transform(new WaitFor(trigger)).listen((v) {print(v);});
trigger.then((_) {print('Future fired');});
}
class WaitFor<T> implements StreamTransformer<T,T> {
final _resultStreamController = new StreamController<T>();
final Future _trigger;
bool _isTriggered = false;
WaitFor(this._trigger);
Stream<T> bind(Stream<T> original) {
original.listen((T value) {
if (_isTriggered) {
_resultStreamController.add(value);
} else {
_trigger.then((_) {
_isTriggered = true;
_resultStreamController.add(value);
});
}
});
return _resultStreamController.stream;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment