Proof of concept for holding off a stream.listen callback until a trigger Future completes.
Last active
August 29, 2015 14:22
-
-
Save natebosch/f9eb8c63e7ece4664b4c to your computer and use it in GitHub Desktop.
This file contains 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'; | |
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