OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 // ------------------------------------------------------------------- | 7 // ------------------------------------------------------------------- |
8 // Core Stream types | 8 // Core Stream types |
9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
10 | 10 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 abstract class Stream<T> { | 55 abstract class Stream<T> { |
56 Stream(); | 56 Stream(); |
57 | 57 |
58 /** | 58 /** |
59 * Creates a new single-subscription stream from the future. | 59 * Creates a new single-subscription stream from the future. |
60 * | 60 * |
61 * When the future completes, the stream will fire one event, either | 61 * When the future completes, the stream will fire one event, either |
62 * data or error, and then close with a done-event. | 62 * data or error, and then close with a done-event. |
63 */ | 63 */ |
64 factory Stream.fromFuture(Future<T> future) { | 64 factory Stream.fromFuture(Future<T> future) { |
65 StreamController<T> controller = new StreamController<T>(); | 65 StreamController<T> controller = new StreamController<T>(sync: true); |
66 future.then((value) { | 66 future.then((value) { |
67 controller.add(value); | 67 controller.add(value); |
68 controller.close(); | 68 controller.close(); |
69 }, | 69 }, |
70 onError: (error) { | 70 onError: (error) { |
71 controller.addError(error); | 71 controller.addError(error); |
72 controller.close(); | 72 controller.close(); |
73 }); | 73 }); |
74 return controller.stream; | 74 return controller.stream; |
75 } | 75 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 controller.add(data); | 107 controller.add(data); |
108 } | 108 } |
109 | 109 |
110 void startPeriodicTimer() { | 110 void startPeriodicTimer() { |
111 assert(timer == null); | 111 assert(timer == null); |
112 timer = new Timer.periodic(period, (Timer timer) { | 112 timer = new Timer.periodic(period, (Timer timer) { |
113 sendEvent(); | 113 sendEvent(); |
114 }); | 114 }); |
115 } | 115 } |
116 | 116 |
117 controller = new StreamController<T>( | 117 controller = new StreamController<T>(sync: true, |
118 onListen: () { | 118 onListen: () { |
119 watch.start(); | 119 watch.start(); |
120 startPeriodicTimer(); | 120 startPeriodicTimer(); |
121 }, | 121 }, |
122 onPause: () { | 122 onPause: () { |
123 timer.cancel(); | 123 timer.cancel(); |
124 timer = null; | 124 timer = null; |
125 watch.stop(); | 125 watch.stop(); |
126 }, | 126 }, |
127 onResume: () { | 127 onResume: () { |
(...skipping 1113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1241 * | 1241 * |
1242 * If a [moveNext] call has been made, it will complete with `false` as value, | 1242 * If a [moveNext] call has been made, it will complete with `false` as value, |
1243 * as will all further calls to [moveNext]. | 1243 * as will all further calls to [moveNext]. |
1244 * | 1244 * |
1245 * If you need to stop listening for values before the stream iterator is | 1245 * If you need to stop listening for values before the stream iterator is |
1246 * automatically closed, you must call [cancel] to ensure that the stream | 1246 * automatically closed, you must call [cancel] to ensure that the stream |
1247 * is properly closed. | 1247 * is properly closed. |
1248 */ | 1248 */ |
1249 void cancel(); | 1249 void cancel(); |
1250 } | 1250 } |
OLD | NEW |