| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 /** Reduces a sequence of values by repeatedly applying [combine]. */ | 127 /** Reduces a sequence of values by repeatedly applying [combine]. */ |
| 128 Future reduce(var initialValue, combine(var previous, T element)) { | 128 Future reduce(var initialValue, combine(var previous, T element)) { |
| 129 Completer completer = new Completer(); | 129 Completer completer = new Completer(); |
| 130 var value = initialValue; | 130 var value = initialValue; |
| 131 StreamSubscription subscription; | 131 StreamSubscription subscription; |
| 132 subscription = this.listen( | 132 subscription = this.listen( |
| 133 (T element) { | 133 (T element) { |
| 134 try { | 134 try { |
| 135 value = combine(value, element); | 135 value = combine(value, element); |
| 136 } catch (e, s) { | 136 } catch (e, s) { |
| 137 subscription.unsubscribe(); | 137 subscription.cancel(); |
| 138 completer.completeError(e, s); | 138 completer.completeError(e, s); |
| 139 } | 139 } |
| 140 }, | 140 }, |
| 141 onError: (AsyncError e) { | 141 onError: (AsyncError e) { |
| 142 completer.completeError(e.error, e.stackTrace); | 142 completer.completeError(e.error, e.stackTrace); |
| 143 }, | 143 }, |
| 144 onDone: () { | 144 onDone: () { |
| 145 completer.complete(value); | 145 completer.complete(value); |
| 146 }, | 146 }, |
| 147 unsubscribeOnError: true); | 147 unsubscribeOnError: true); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 167 | 167 |
| 168 /** | 168 /** |
| 169 * Check whether [match] occurs in the elements provided by this stream. | 169 * Check whether [match] occurs in the elements provided by this stream. |
| 170 * | 170 * |
| 171 * Completes the [Future] when the answer is known. | 171 * Completes the [Future] when the answer is known. |
| 172 * If this stream reports an error, the [Future] will report that error. | 172 * If this stream reports an error, the [Future] will report that error. |
| 173 */ | 173 */ |
| 174 Future<bool> contains(T match) { | 174 Future<bool> contains(T match) { |
| 175 _FutureImpl<bool> future = new _FutureImpl<bool>(); | 175 _FutureImpl<bool> future = new _FutureImpl<bool>(); |
| 176 StreamSubscription subscription; | 176 StreamSubscription subscription; |
| 177 subscription = listen( | 177 subscription = this.listen( |
| 178 (T element) { | 178 (T element) { |
| 179 if (element == match) { | 179 if (element == match) { |
| 180 subscription.unsubscribe(); | 180 subscription.cancel(); |
| 181 future._setValue(true); | 181 future._setValue(true); |
| 182 } | 182 } |
| 183 }, | 183 }, |
| 184 onError: future._setError, | 184 onError: future._setError, |
| 185 onDone: () { | 185 onDone: () { |
| 186 future._setValue(false); | 186 future._setValue(false); |
| 187 }, | 187 }, |
| 188 unsubscribeOnError: true); | 188 unsubscribeOnError: true); |
| 189 return future; | 189 return future; |
| 190 } | 190 } |
| 191 | 191 |
| 192 /** | 192 /** |
| 193 * Check whether [test] accepts all elements provided by this stream. | 193 * Check whether [test] accepts all elements provided by this stream. |
| 194 * | 194 * |
| 195 * Completes the [Future] when the answer is known. | 195 * Completes the [Future] when the answer is known. |
| 196 * If this stream reports an error, the [Future] will report that error. | 196 * If this stream reports an error, the [Future] will report that error. |
| 197 */ | 197 */ |
| 198 Future<bool> every(bool test(T element)) { | 198 Future<bool> every(bool test(T element)) { |
| 199 _FutureImpl<bool> future = new _FutureImpl<bool>(); | 199 _FutureImpl<bool> future = new _FutureImpl<bool>(); |
| 200 StreamSubscription subscription; | 200 StreamSubscription subscription; |
| 201 subscription = listen( | 201 subscription = this.listen( |
| 202 (T element) { | 202 (T element) { |
| 203 if (!test(element)) { | 203 if (!test(element)) { |
| 204 subscription.unsubscribe(); | 204 subscription.cancel(); |
| 205 future._setValue(false); | 205 future._setValue(false); |
| 206 } | 206 } |
| 207 }, | 207 }, |
| 208 onError: future._setError, | 208 onError: future._setError, |
| 209 onDone: () { | 209 onDone: () { |
| 210 future._setValue(true); | 210 future._setValue(true); |
| 211 }, | 211 }, |
| 212 unsubscribeOnError: true); | 212 unsubscribeOnError: true); |
| 213 return future; | 213 return future; |
| 214 } | 214 } |
| 215 | 215 |
| 216 /** | 216 /** |
| 217 * Check whether [test] accepts any element provided by this stream. | 217 * Check whether [test] accepts any element provided by this stream. |
| 218 * | 218 * |
| 219 * Completes the [Future] when the answer is known. | 219 * Completes the [Future] when the answer is known. |
| 220 * If this stream reports an error, the [Future] will report that error. | 220 * If this stream reports an error, the [Future] will report that error. |
| 221 */ | 221 */ |
| 222 Future<bool> any(bool test(T element)) { | 222 Future<bool> any(bool test(T element)) { |
| 223 _FutureImpl<bool> future = new _FutureImpl<bool>(); | 223 _FutureImpl<bool> future = new _FutureImpl<bool>(); |
| 224 StreamSubscription subscription; | 224 StreamSubscription subscription; |
| 225 subscription = listen( | 225 subscription = this.listen( |
| 226 (T element) { | 226 (T element) { |
| 227 if (test(element)) { | 227 if (test(element)) { |
| 228 subscription.unsubscribe(); | 228 subscription.cancel(); |
| 229 future._setValue(true); | 229 future._setValue(true); |
| 230 } | 230 } |
| 231 }, | 231 }, |
| 232 onError: future._setError, | 232 onError: future._setError, |
| 233 onDone: () { | 233 onDone: () { |
| 234 future._setValue(false); | 234 future._setValue(false); |
| 235 }, | 235 }, |
| 236 unsubscribeOnError: true); | 236 unsubscribeOnError: true); |
| 237 return future; | 237 return future; |
| 238 } | 238 } |
| 239 | 239 |
| 240 | 240 |
| 241 /** Counts the elements in the stream. */ | 241 /** Counts the elements in the stream. */ |
| 242 Future<int> get length { | 242 Future<int> get length { |
| 243 _FutureImpl<int> future = new _FutureImpl<int>(); | 243 _FutureImpl<int> future = new _FutureImpl<int>(); |
| 244 int count = 0; | 244 int count = 0; |
| 245 listen( | 245 this.listen( |
| 246 (_) { count++; }, | 246 (_) { count++; }, |
| 247 onError: future._setError, | 247 onError: future._setError, |
| 248 onDone: () { | 248 onDone: () { |
| 249 future._setValue(count); | 249 future._setValue(count); |
| 250 }, | 250 }, |
| 251 unsubscribeOnError: true); | 251 unsubscribeOnError: true); |
| 252 return future; | 252 return future; |
| 253 } | 253 } |
| 254 | 254 |
| 255 /** | 255 /** |
| 256 * Finds the least element in the stream. | 256 * Finds the least element in the stream. |
| 257 * | 257 * |
| 258 * If the stream is empty, the result is [:null:]. | 258 * If the stream is empty, the result is [:null:]. |
| 259 * Otherwise the result is a value from the stream that is not greater | 259 * Otherwise the result is a value from the stream that is not greater |
| 260 * than any other value from the stream (according to [compare], which must | 260 * than any other value from the stream (according to [compare], which must |
| 261 * be a [Comparator]). | 261 * be a [Comparator]). |
| 262 * | 262 * |
| 263 * If [compare] is omitted, it defaults to [Comparable.compare]. | 263 * If [compare] is omitted, it defaults to [Comparable.compare]. |
| 264 */ | 264 */ |
| 265 Future<T> min([int compare(T a, T b)]) { | 265 Future<T> min([int compare(T a, T b)]) { |
| 266 if (compare == null) compare = Comparable.compare; | 266 if (compare == null) compare = Comparable.compare; |
| 267 _FutureImpl<T> future = new _FutureImpl<T>(); | 267 _FutureImpl<T> future = new _FutureImpl<T>(); |
| 268 StreamSubscription subscription; | 268 StreamSubscription subscription; |
| 269 T min = null; | 269 T min = null; |
| 270 subscription = listen( | 270 subscription = this.listen( |
| 271 (T value) { | 271 (T value) { |
| 272 min = value; | 272 min = value; |
| 273 subscription.onData((T value) { | 273 subscription.onData((T value) { |
| 274 if (compare(min, value) > 0) min = value; | 274 if (compare(min, value) > 0) min = value; |
| 275 }); | 275 }); |
| 276 }, | 276 }, |
| 277 onError: future._setError, | 277 onError: future._setError, |
| 278 onDone: () { | 278 onDone: () { |
| 279 future._setValue(min); | 279 future._setValue(min); |
| 280 }, | 280 }, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 291 * than any other value from the stream (according to [compare], which must | 291 * than any other value from the stream (according to [compare], which must |
| 292 * be a [Comparator]). | 292 * be a [Comparator]). |
| 293 * | 293 * |
| 294 * If [compare] is omitted, it defaults to [Comparable.compare]. | 294 * If [compare] is omitted, it defaults to [Comparable.compare]. |
| 295 */ | 295 */ |
| 296 Future<T> max([int compare(T a, T b)]) { | 296 Future<T> max([int compare(T a, T b)]) { |
| 297 if (compare == null) compare = Comparable.compare; | 297 if (compare == null) compare = Comparable.compare; |
| 298 _FutureImpl<T> future = new _FutureImpl<T>(); | 298 _FutureImpl<T> future = new _FutureImpl<T>(); |
| 299 StreamSubscription subscription; | 299 StreamSubscription subscription; |
| 300 T max = null; | 300 T max = null; |
| 301 subscription = listen( | 301 subscription = this.listen( |
| 302 (T value) { | 302 (T value) { |
| 303 max = value; | 303 max = value; |
| 304 subscription.onData((T value) { | 304 subscription.onData((T value) { |
| 305 if (compare(max, value) < 0) max = value; | 305 if (compare(max, value) < 0) max = value; |
| 306 }); | 306 }); |
| 307 }, | 307 }, |
| 308 onError: future._setError, | 308 onError: future._setError, |
| 309 onDone: () { | 309 onDone: () { |
| 310 future._setValue(max); | 310 future._setValue(max); |
| 311 }, | 311 }, |
| 312 unsubscribeOnError: true | 312 unsubscribeOnError: true |
| 313 ); | 313 ); |
| 314 return future; | 314 return future; |
| 315 } | 315 } |
| 316 | 316 |
| 317 /** Reports whether this stream contains any elements. */ | 317 /** Reports whether this stream contains any elements. */ |
| 318 Future<bool> get isEmpty { | 318 Future<bool> get isEmpty { |
| 319 _FutureImpl<bool> future = new _FutureImpl<bool>(); | 319 _FutureImpl<bool> future = new _FutureImpl<bool>(); |
| 320 StreamSubscription subscription; | 320 StreamSubscription subscription; |
| 321 subscription = listen( | 321 subscription = this.listen( |
| 322 (_) { | 322 (_) { |
| 323 subscription.unsubscribe(); | 323 subscription.cancel(); |
| 324 future._setValue(false); | 324 future._setValue(false); |
| 325 }, | 325 }, |
| 326 onError: future._setError, | 326 onError: future._setError, |
| 327 onDone: () { | 327 onDone: () { |
| 328 future._setValue(true); | 328 future._setValue(true); |
| 329 }, | 329 }, |
| 330 unsubscribeOnError: true); | 330 unsubscribeOnError: true); |
| 331 return future; | 331 return future; |
| 332 } | 332 } |
| 333 | 333 |
| 334 /** Collect the data of this stream in a [List]. */ | 334 /** Collect the data of this stream in a [List]. */ |
| 335 Future<List<T>> toList() { | 335 Future<List<T>> toList() { |
| 336 List<T> result = <T>[]; | 336 List<T> result = <T>[]; |
| 337 _FutureImpl<List<T>> future = new _FutureImpl<List<T>>(); | 337 _FutureImpl<List<T>> future = new _FutureImpl<List<T>>(); |
| 338 listen( | 338 this.listen( |
| 339 (T data) { | 339 (T data) { |
| 340 result.add(data); | 340 result.add(data); |
| 341 }, | 341 }, |
| 342 onError: future._setError, | 342 onError: future._setError, |
| 343 onDone: () { | 343 onDone: () { |
| 344 future._setValue(result); | 344 future._setValue(result); |
| 345 }, | 345 }, |
| 346 unsubscribeOnError: true); | 346 unsubscribeOnError: true); |
| 347 return future; | 347 return future; |
| 348 } | 348 } |
| 349 | 349 |
| 350 /** Collect the data of this stream in a [Set]. */ | 350 /** Collect the data of this stream in a [Set]. */ |
| 351 Future<Set<T>> toSet() { | 351 Future<Set<T>> toSet() { |
| 352 Set<T> result = new Set<T>(); | 352 Set<T> result = new Set<T>(); |
| 353 _FutureImpl<Set<T>> future = new _FutureImpl<Set<T>>(); | 353 _FutureImpl<Set<T>> future = new _FutureImpl<Set<T>>(); |
| 354 listen( | 354 this.listen( |
| 355 (T data) { | 355 (T data) { |
| 356 result.add(data); | 356 result.add(data); |
| 357 }, | 357 }, |
| 358 onError: future._setError, | 358 onError: future._setError, |
| 359 onDone: () { | 359 onDone: () { |
| 360 future._setValue(result); | 360 future._setValue(result); |
| 361 }, | 361 }, |
| 362 unsubscribeOnError: true); | 362 unsubscribeOnError: true); |
| 363 return future; | 363 return future; |
| 364 } | 364 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 | 422 |
| 423 /** | 423 /** |
| 424 * Returns the first element. | 424 * Returns the first element. |
| 425 * | 425 * |
| 426 * If [this] is empty throws a [StateError]. Otherwise this method is | 426 * If [this] is empty throws a [StateError]. Otherwise this method is |
| 427 * equivalent to [:this.elementAt(0):] | 427 * equivalent to [:this.elementAt(0):] |
| 428 */ | 428 */ |
| 429 Future<T> get first { | 429 Future<T> get first { |
| 430 _FutureImpl<T> future = new _FutureImpl(); | 430 _FutureImpl<T> future = new _FutureImpl(); |
| 431 StreamSubscription subscription; | 431 StreamSubscription subscription; |
| 432 subscription = listen( | 432 subscription = this.listen( |
| 433 (T value) { | 433 (T value) { |
| 434 future._setValue(value); | 434 future._setValue(value); |
| 435 subscription.unsubscribe(); | 435 subscription.cancel(); |
| 436 return; | 436 return; |
| 437 }, | 437 }, |
| 438 onError: future._setError, | 438 onError: future._setError, |
| 439 onDone: () { | 439 onDone: () { |
| 440 future._setError(new AsyncError(new StateError("No elements"))); | 440 future._setError(new AsyncError(new StateError("No elements"))); |
| 441 }, | 441 }, |
| 442 unsubscribeOnError: true); | 442 unsubscribeOnError: true); |
| 443 return future; | 443 return future; |
| 444 } | 444 } |
| 445 | 445 |
| 446 /** | 446 /** |
| 447 * Returns the last element. | 447 * Returns the last element. |
| 448 * | 448 * |
| 449 * If [this] is empty throws a [StateError]. | 449 * If [this] is empty throws a [StateError]. |
| 450 */ | 450 */ |
| 451 Future<T> get last { | 451 Future<T> get last { |
| 452 _FutureImpl<T> future = new _FutureImpl<T>(); | 452 _FutureImpl<T> future = new _FutureImpl<T>(); |
| 453 T result = null; | 453 T result = null; |
| 454 bool foundResult = false; | 454 bool foundResult = false; |
| 455 StreamSubscription subscription; | 455 StreamSubscription subscription; |
| 456 subscription = listen( | 456 subscription = this.listen( |
| 457 (T value) { | 457 (T value) { |
| 458 foundResult = true; | 458 foundResult = true; |
| 459 result = value; | 459 result = value; |
| 460 }, | 460 }, |
| 461 onError: future._setError, | 461 onError: future._setError, |
| 462 onDone: () { | 462 onDone: () { |
| 463 if (foundResult) { | 463 if (foundResult) { |
| 464 future._setValue(result); | 464 future._setValue(result); |
| 465 return; | 465 return; |
| 466 } | 466 } |
| 467 future._setError(new AsyncError(new StateError("No elements"))); | 467 future._setError(new AsyncError(new StateError("No elements"))); |
| 468 }, | 468 }, |
| 469 unsubscribeOnError: true); | 469 unsubscribeOnError: true); |
| 470 return future; | 470 return future; |
| 471 } | 471 } |
| 472 | 472 |
| 473 /** | 473 /** |
| 474 * Returns the single element. | 474 * Returns the single element. |
| 475 * | 475 * |
| 476 * If [this] is empty or has more than one element throws a [StateError]. | 476 * If [this] is empty or has more than one element throws a [StateError]. |
| 477 */ | 477 */ |
| 478 Future<T> get single { | 478 Future<T> get single { |
| 479 _FutureImpl<T> future = new _FutureImpl<T>(); | 479 _FutureImpl<T> future = new _FutureImpl<T>(); |
| 480 T result = null; | 480 T result = null; |
| 481 bool foundResult = false; | 481 bool foundResult = false; |
| 482 StreamSubscription subscription; | 482 StreamSubscription subscription; |
| 483 subscription = listen( | 483 subscription = this.listen( |
| 484 (T value) { | 484 (T value) { |
| 485 if (foundResult) { | 485 if (foundResult) { |
| 486 // This is the second element we get. | 486 // This is the second element we get. |
| 487 Error error = new StateError("More than one element"); | 487 Error error = new StateError("More than one element"); |
| 488 future._setError(new AsyncError(error)); | 488 future._setError(new AsyncError(error)); |
| 489 subscription.unsubscribe(); | 489 subscription.cancel(); |
| 490 return; | 490 return; |
| 491 } | 491 } |
| 492 foundResult = true; | 492 foundResult = true; |
| 493 result = value; | 493 result = value; |
| 494 }, | 494 }, |
| 495 onError: future._setError, | 495 onError: future._setError, |
| 496 onDone: () { | 496 onDone: () { |
| 497 if (foundResult) { | 497 if (foundResult) { |
| 498 future._setValue(result); | 498 future._setValue(result); |
| 499 return; | 499 return; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 514 * [defaultValue] function is provided, the result of calling [defaultValue] | 514 * [defaultValue] function is provided, the result of calling [defaultValue] |
| 515 * becomes the value of the future. | 515 * becomes the value of the future. |
| 516 * | 516 * |
| 517 * If an error occurs, or if this stream ends without finding a match and | 517 * If an error occurs, or if this stream ends without finding a match and |
| 518 * with no [defaultValue] function provided, the future will receive an | 518 * with no [defaultValue] function provided, the future will receive an |
| 519 * error. | 519 * error. |
| 520 */ | 520 */ |
| 521 Future<T> firstMatching(bool test(T value), {T defaultValue()}) { | 521 Future<T> firstMatching(bool test(T value), {T defaultValue()}) { |
| 522 _FutureImpl<T> future = new _FutureImpl<T>(); | 522 _FutureImpl<T> future = new _FutureImpl<T>(); |
| 523 StreamSubscription subscription; | 523 StreamSubscription subscription; |
| 524 subscription = listen( | 524 subscription = this.listen( |
| 525 (T value) { | 525 (T value) { |
| 526 bool matches; | 526 bool matches; |
| 527 try { | 527 try { |
| 528 matches = (true == test(value)); | 528 matches = (true == test(value)); |
| 529 } catch (e, s) { | 529 } catch (e, s) { |
| 530 future._setError(new AsyncError(e, s)); | 530 future._setError(new AsyncError(e, s)); |
| 531 subscription.unsubscribe(); | 531 subscription.cancel(); |
| 532 return; | 532 return; |
| 533 } | 533 } |
| 534 if (matches) { | 534 if (matches) { |
| 535 future._setValue(value); | 535 future._setValue(value); |
| 536 subscription.unsubscribe(); | 536 subscription.cancel(); |
| 537 } | 537 } |
| 538 }, | 538 }, |
| 539 onError: future._setError, | 539 onError: future._setError, |
| 540 onDone: () { | 540 onDone: () { |
| 541 if (defaultValue != null) { | 541 if (defaultValue != null) { |
| 542 T value; | 542 T value; |
| 543 try { | 543 try { |
| 544 value = defaultValue(); | 544 value = defaultValue(); |
| 545 } catch (e, s) { | 545 } catch (e, s) { |
| 546 future._setError(new AsyncError(e, s)); | 546 future._setError(new AsyncError(e, s)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 561 * | 561 * |
| 562 * As [firstMatching], except that the last matching element is found. | 562 * As [firstMatching], except that the last matching element is found. |
| 563 * That means that the result cannot be provided before this stream | 563 * That means that the result cannot be provided before this stream |
| 564 * is done. | 564 * is done. |
| 565 */ | 565 */ |
| 566 Future<T> lastMatching(bool test(T value), {T defaultValue()}) { | 566 Future<T> lastMatching(bool test(T value), {T defaultValue()}) { |
| 567 _FutureImpl<T> future = new _FutureImpl<T>(); | 567 _FutureImpl<T> future = new _FutureImpl<T>(); |
| 568 T result = null; | 568 T result = null; |
| 569 bool foundResult = false; | 569 bool foundResult = false; |
| 570 StreamSubscription subscription; | 570 StreamSubscription subscription; |
| 571 subscription = listen( | 571 subscription = this.listen( |
| 572 (T value) { | 572 (T value) { |
| 573 bool matches; | 573 bool matches; |
| 574 try { | 574 try { |
| 575 matches = (true == test(value)); | 575 matches = (true == test(value)); |
| 576 } catch (e, s) { | 576 } catch (e, s) { |
| 577 future._setError(new AsyncError(e, s)); | 577 future._setError(new AsyncError(e, s)); |
| 578 subscription.unsubscribe(); | 578 subscription.cancel(); |
| 579 return; | 579 return; |
| 580 } | 580 } |
| 581 if (matches) { | 581 if (matches) { |
| 582 foundResult = true; | 582 foundResult = true; |
| 583 result = value; | 583 result = value; |
| 584 } | 584 } |
| 585 }, | 585 }, |
| 586 onError: future._setError, | 586 onError: future._setError, |
| 587 onDone: () { | 587 onDone: () { |
| 588 if (foundResult) { | 588 if (foundResult) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 611 * Finds the single element in this stream matching [test]. | 611 * Finds the single element in this stream matching [test]. |
| 612 * | 612 * |
| 613 * Like [lastMatch], except that it is an error if more than one | 613 * Like [lastMatch], except that it is an error if more than one |
| 614 * matching element occurs in the stream. | 614 * matching element occurs in the stream. |
| 615 */ | 615 */ |
| 616 Future<T> singleMatching(bool test(T value)) { | 616 Future<T> singleMatching(bool test(T value)) { |
| 617 _FutureImpl<T> future = new _FutureImpl<T>(); | 617 _FutureImpl<T> future = new _FutureImpl<T>(); |
| 618 T result = null; | 618 T result = null; |
| 619 bool foundResult = false; | 619 bool foundResult = false; |
| 620 StreamSubscription subscription; | 620 StreamSubscription subscription; |
| 621 subscription = listen( | 621 subscription = this.listen( |
| 622 (T value) { | 622 (T value) { |
| 623 bool matches; | 623 bool matches; |
| 624 try { | 624 try { |
| 625 matches = (true == test(value)); | 625 matches = (true == test(value)); |
| 626 } catch (e, s) { | 626 } catch (e, s) { |
| 627 future._setError(new AsyncError(e, s)); | 627 future._setError(new AsyncError(e, s)); |
| 628 subscription.unsubscribe(); | 628 subscription.cancel(); |
| 629 return; | 629 return; |
| 630 } | 630 } |
| 631 if (matches) { | 631 if (matches) { |
| 632 if (foundResult) { | 632 if (foundResult) { |
| 633 future._setError(new AsyncError( | 633 future._setError(new AsyncError( |
| 634 new StateError('Multiple matches for "single"'))); | 634 new StateError('Multiple matches for "single"'))); |
| 635 subscription.unsubscribe(); | 635 subscription.cancel(); |
| 636 return; | 636 return; |
| 637 } | 637 } |
| 638 foundResult = true; | 638 foundResult = true; |
| 639 result = value; | 639 result = value; |
| 640 } | 640 } |
| 641 }, | 641 }, |
| 642 onError: future._setError, | 642 onError: future._setError, |
| 643 onDone: () { | 643 onDone: () { |
| 644 if (foundResult) { | 644 if (foundResult) { |
| 645 future._setValue(result); | 645 future._setValue(result); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 656 * Returns the value of the [index]th data event of this stream. | 656 * Returns the value of the [index]th data event of this stream. |
| 657 * | 657 * |
| 658 * If an error event occurs, the future will end with this error. | 658 * If an error event occurs, the future will end with this error. |
| 659 * | 659 * |
| 660 * If this stream provides fewer than [index] elements before closing, | 660 * If this stream provides fewer than [index] elements before closing, |
| 661 * an error is reported. | 661 * an error is reported. |
| 662 */ | 662 */ |
| 663 Future<T> elementAt(int index) { | 663 Future<T> elementAt(int index) { |
| 664 _FutureImpl<T> future = new _FutureImpl(); | 664 _FutureImpl<T> future = new _FutureImpl(); |
| 665 StreamSubscription subscription; | 665 StreamSubscription subscription; |
| 666 subscription = listen( | 666 subscription = this.listen( |
| 667 (T value) { | 667 (T value) { |
| 668 if (index == 0) { | 668 if (index == 0) { |
| 669 future._setValue(value); | 669 future._setValue(value); |
| 670 subscription.unsubscribe(); | 670 subscription.cancel(); |
| 671 return; | 671 return; |
| 672 } | 672 } |
| 673 index -= 1; | 673 index -= 1; |
| 674 }, | 674 }, |
| 675 onError: future._setError, | 675 onError: future._setError, |
| 676 onDone: () { | 676 onDone: () { |
| 677 future._setError(new AsyncError( | 677 future._setError(new AsyncError( |
| 678 new StateError("Not enough elements for elementAt"))); | 678 new StateError("Not enough elements for elementAt"))); |
| 679 }, | 679 }, |
| 680 unsubscribeOnError: true); | 680 unsubscribeOnError: true); |
| 681 return future; | 681 return future; |
| 682 } | 682 } |
| 683 } | 683 } |
| 684 | 684 |
| 685 /** | 685 /** |
| 686 * A control object for the subscription on a [Stream]. | 686 * A control object for the subscription on a [Stream]. |
| 687 * | 687 * |
| 688 * When you subscribe on a [Stream] using [Stream.subscribe], | 688 * When you subscribe on a [Stream] using [Stream.subscribe], |
| 689 * a [StreamSubscription] object is returned. This object | 689 * a [StreamSubscription] object is returned. This object |
| 690 * is used to later unsubscribe again, or to temporarily pause | 690 * is used to later unsubscribe again, or to temporarily pause |
| 691 * the stream's events. | 691 * the stream's events. |
| 692 */ | 692 */ |
| 693 abstract class StreamSubscription<T> { | 693 abstract class StreamSubscription<T> { |
| 694 /** | 694 /** |
| 695 * Cancels this subscription. It will no longer receive events. | 695 * Cancels this subscription. It will no longer receive events. |
| 696 * | 696 * |
| 697 * If an event is currently firing, this unsubscription will only | 697 * If an event is currently firing, this unsubscription will only |
| 698 * take effect after all subscribers have received the current event. | 698 * take effect after all subscribers have received the current event. |
| 699 */ | 699 */ |
| 700 void unsubscribe(); | 700 void cancel(); |
| 701 | 701 |
| 702 /** Set or override the data event handler of this subscription. */ | 702 /** Set or override the data event handler of this subscription. */ |
| 703 void onData(void handleData(T data)); | 703 void onData(void handleData(T data)); |
| 704 | 704 |
| 705 /** Set or override the error event handler of this subscription. */ | 705 /** Set or override the error event handler of this subscription. */ |
| 706 void onError(void handleError(AsyncError error)); | 706 void onError(void handleError(AsyncError error)); |
| 707 | 707 |
| 708 /** Set or override the done event handler of this subscription. */ | 708 /** Set or override the done event handler of this subscription. */ |
| 709 void onDone(void handleDone()); | 709 void onDone(void handleDone()); |
| 710 | 710 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 sink.signalError(error); | 829 sink.signalError(error); |
| 830 } | 830 } |
| 831 | 831 |
| 832 /** | 832 /** |
| 833 * Handle an incoming done event. | 833 * Handle an incoming done event. |
| 834 */ | 834 */ |
| 835 void handleDone(StreamSink<T> sink) { | 835 void handleDone(StreamSink<T> sink) { |
| 836 sink.close(); | 836 sink.close(); |
| 837 } | 837 } |
| 838 } | 838 } |
| OLD | NEW |