| 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 import 'package:expect/expect.dart'; | 5 import 'package:expect/expect.dart'; |
| 6 import 'package:async_helper/async_helper.dart'; | 6 import 'package:async_helper/async_helper.dart'; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'event_helper.dart'; | 8 import 'event_helper.dart'; |
| 9 | 9 |
| 10 | 10 |
| 11 class DecrementingTransformerSink implements EventSink { | 11 class DecrementingTransformerSink implements EventSink { |
| 12 final outSink; | 12 final outSink; |
| 13 DecrementingTransformerSink(this.outSink); | 13 DecrementingTransformerSink(this.outSink); |
| 14 | 14 |
| 15 void add(int i) => outSink.add(i - 1); | 15 void add(int i) => outSink.add(i - 1); |
| 16 void addError(int e, st) => outSink.addError(e - 1, st); | 16 void addError(int e, [st]) => outSink.addError(e - 1, st); |
| 17 void close() => outSink.close(); | 17 void close() => outSink.close(); |
| 18 } | 18 } |
| 19 | 19 |
| 20 class FutureWaitingTransformerSink implements EventSink { | 20 class FutureWaitingTransformerSink implements EventSink { |
| 21 final outSink; | 21 final outSink; |
| 22 final closeFuture; | 22 final closeFuture; |
| 23 FutureWaitingTransformerSink(this.outSink, this.closeFuture); | 23 FutureWaitingTransformerSink(this.outSink, this.closeFuture); |
| 24 | 24 |
| 25 void add(Future future) { future.then(outSink.add); } | 25 void add(Future future) { future.then(outSink.add); } |
| 26 void addError(Future e, st) { e.then((val) { outSink.addError(val, st); }); } | 26 void addError(Future e, [st]) { e.then((val) { outSink.addError(val, st); });
} |
| 27 void close() { closeFuture.whenComplete(outSink.close); } | 27 void close() { closeFuture.whenComplete(outSink.close); } |
| 28 } | 28 } |
| 29 | 29 |
| 30 class ZoneTransformerSink implements EventSink { | 30 class ZoneTransformerSink implements EventSink { |
| 31 final outSink; | 31 final outSink; |
| 32 ZoneTransformerSink(this.outSink); | 32 ZoneTransformerSink(this.outSink); |
| 33 | 33 |
| 34 void add(_) { outSink.add(Zone.current); } | 34 void add(_) { outSink.add(Zone.current); } |
| 35 void addError(_, st) { outSink.add(Zone.current); } | 35 void addError(_, [st]) { outSink.add(Zone.current); } |
| 36 void close() { | 36 void close() { |
| 37 outSink.add(Zone.current); | 37 outSink.add(Zone.current); |
| 38 outSink.close(); | 38 outSink.close(); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 class TypeChangingSink implements EventSink<int> { | 42 class TypeChangingSink implements EventSink<int> { |
| 43 final EventSink<String> outSink; | 43 final EventSink<String> outSink; |
| 44 TypeChangingSink(this.outSink); | 44 TypeChangingSink(this.outSink); |
| 45 | 45 |
| 46 void add(int data) { outSink.add(data.toString()); } | 46 void add(int data) { outSink.add(data.toString()); } |
| 47 void addError(error, st) { outSink.addError(error, st); } | 47 void addError(error, [st]) { outSink.addError(error, st); } |
| 48 void close() { outSink.close(); } | 48 void close() { outSink.close(); } |
| 49 } | 49 } |
| 50 | 50 |
| 51 class SinkTransformer<S, T> implements StreamTransformer<S, T> { | 51 class SinkTransformer<S, T> implements StreamTransformer<S, T> { |
| 52 final Function sinkMapper; | 52 final Function sinkMapper; |
| 53 SinkTransformer(this.sinkMapper); | 53 SinkTransformer(this.sinkMapper); |
| 54 | 54 |
| 55 Stream<T> bind(Stream<S> stream) { | 55 Stream<T> bind(Stream<S> stream) { |
| 56 return new Stream<T>.eventTransformed(stream, sinkMapper); | 56 return new Stream<T>.eventTransformed(stream, sinkMapper); |
| 57 } | 57 } |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 new Stream.fromIterable([1, 2, 3]) | 304 new Stream.fromIterable([1, 2, 3]) |
| 305 .transform(new SinkTransformer<int, String>( | 305 .transform(new SinkTransformer<int, String>( |
| 306 (sink) => new TypeChangingSink(sink))) | 306 (sink) => new TypeChangingSink(sink))) |
| 307 .toList() | 307 .toList() |
| 308 .then((list) { | 308 .then((list) { |
| 309 Expect.listEquals(["1", "2", "3"], list); | 309 Expect.listEquals(["1", "2", "3"], list); |
| 310 asyncEnd(); | 310 asyncEnd(); |
| 311 }); | 311 }); |
| 312 } | 312 } |
| 313 } | 313 } |
| OLD | NEW |