Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Streams, StreamController, StreamSink
- import "dart:async";
- import "dart:math";
- import "dart:html";
- /// stream - the object that blindly sends out data
- /// streamcontroller - adds to the stream, and tells the status of the stream
- // can close only if not actively sending data
- /// streamsink - a hole to put stuff into it, like a stream - not always needed, implements streamconsumer
- /// streamconsumer - abstract - superclass for sink
- /// streamsubscription - abstract - superclass for listen - created when some type of listening is done
- /// streamTransformer - ???
- /// completer - not sure if needed
- void main() {
- InputElement submit = querySelector("#submit");
- InputElement add1 = querySelector("#add1");
- InputElement add2 = querySelector("#add2");
- InputElement remove = querySelector("#remove");
- Stream<int> a = _ints();
- StreamController<dynamic> sc = StreamController();
- sc.stream.listen((e) {
- print(e);
- });
- add1.onClick.listen((e) {
- sc.addStream(a);
- });
- add2.onClick.listen((e) {
- sc.sink.add("I added something");
- });
- remove.onClick.listen((e) {
- sc.close(); //can only close when data not actively streaming
- });
- submit.onClick.listen((e) {
- sc.addError("oops");
- });
- }
- Stream<int> _numbers() async* {
- for (int i = 0; i < 10; i++) {
- yield i;
- }
- }
- Stream<int> _ints() async* {
- yield* Stream.periodic(Duration(seconds: 1), (int a) {
- return a++;
- });
- }
- Stream<String> _strings() async* {
- String a = "qwertyuiopasdfghjklzxcvbnm";
- Random r = Random();
- yield* Stream.periodic(Duration(seconds: 2), (_) {
- return a[r.nextInt(a.length)];
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement