Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. // Streams, StreamController, StreamSink
  2. import "dart:async";
  3. import "dart:math";
  4. import "dart:html";
  5.  
  6. /// stream - the object that blindly sends out data
  7. /// streamcontroller - adds to the stream, and tells the status of the stream
  8. // can close only if not actively sending data
  9. /// streamsink - a hole to put stuff into it, like a stream - not always needed, implements streamconsumer
  10. /// streamconsumer - abstract - superclass for sink
  11. /// streamsubscription - abstract - superclass for listen - created when some type of listening is done
  12. /// streamTransformer - ???
  13. /// completer - not sure if needed
  14.  
  15. void main() {
  16. InputElement submit = querySelector("#submit");
  17. InputElement add1 = querySelector("#add1");
  18. InputElement add2 = querySelector("#add2");
  19. InputElement remove = querySelector("#remove");
  20. Stream<int> a = _ints();
  21. StreamController<dynamic> sc = StreamController();
  22. sc.stream.listen((e) {
  23. print(e);
  24. });
  25.  
  26. add1.onClick.listen((e) {
  27. sc.addStream(a);
  28. });
  29. add2.onClick.listen((e) {
  30. sc.sink.add("I added something");
  31. });
  32. remove.onClick.listen((e) {
  33. sc.close(); //can only close when data not actively streaming
  34. });
  35. submit.onClick.listen((e) {
  36. sc.addError("oops");
  37. });
  38. }
  39.  
  40. Stream<int> _numbers() async* {
  41. for (int i = 0; i < 10; i++) {
  42. yield i;
  43. }
  44. }
  45.  
  46. Stream<int> _ints() async* {
  47. yield* Stream.periodic(Duration(seconds: 1), (int a) {
  48. return a++;
  49. });
  50. }
  51.  
  52. Stream<String> _strings() async* {
  53. String a = "qwertyuiopasdfghjklzxcvbnm";
  54. Random r = Random();
  55. yield* Stream.periodic(Duration(seconds: 2), (_) {
  56. return a[r.nextInt(a.length)];
  57. });
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement