| 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 library event_helper; |    5 library event_helper; | 
|    6  |    6  | 
|    7 import 'dart:async'; |    7 import 'dart:async'; | 
|    8  |    8  | 
|    9 abstract class Event { |    9 abstract class Event { | 
|   10   void replay(EventSink sink); |   10   void replay(EventSink sink); | 
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   54   int get hashCode => 42; |   54   int get hashCode => 42; | 
|   55  |   55  | 
|   56   bool operator==(Object other) => other is DoneEvent; |   56   bool operator==(Object other) => other is DoneEvent; | 
|   57  |   57  | 
|   58   String toString() => "DoneEvent"; |   58   String toString() => "DoneEvent"; | 
|   59 } |   59 } | 
|   60  |   60  | 
|   61 /** Collector of events. */ |   61 /** Collector of events. */ | 
|   62 class Events implements EventSink { |   62 class Events implements EventSink { | 
|   63   final List<Event> events = []; |   63   final List<Event> events = []; | 
 |   64   bool trace = false; | 
|   64  |   65  | 
|   65   Events(); |   66   Events(); | 
 |   67  | 
|   66   Events.fromIterable(Iterable iterable) { |   68   Events.fromIterable(Iterable iterable) { | 
|   67     for (var value in iterable) add(value); |   69     for (var value in iterable) add(value); | 
|   68     close(); |   70     close(); | 
|   69   } |   71   } | 
|   70  |   72  | 
|   71   /** Capture events from a stream into a new [Events] object. */ |   73   /** Capture events from a stream into a new [Events] object. */ | 
|   72   factory Events.capture(Stream stream, |   74   factory Events.capture(Stream stream, | 
|   73                          { bool cancelOnError: false }) = CaptureEvents; |   75                          { bool cancelOnError: false }) = CaptureEvents; | 
|   74  |   76  | 
|   75   // EventSink interface. |   77   // EventSink interface. | 
|   76   void add(var value) { |   78   void add(var value) { | 
 |   79     if (trace) print("Events#$hashCode: add($value)"); | 
|   77     events.add(new DataEvent(value)); |   80     events.add(new DataEvent(value)); | 
|   78   } |   81   } | 
|   79  |   82  | 
|   80   void addError(error) { |   83   void addError(error) { | 
 |   84     if (trace) print("Events#$hashCode: addError($error)"); | 
|   81     events.add(new ErrorEvent(error)); |   85     events.add(new ErrorEvent(error)); | 
|   82   } |   86   } | 
|   83  |   87  | 
|   84   void close() { |   88   void close() { | 
 |   89     if (trace) print("Events#$hashCode: close()"); | 
|   85     events.add(const DoneEvent()); |   90     events.add(const DoneEvent()); | 
|   86   } |   91   } | 
|   87  |   92  | 
|   88   // Error helper for creating errors manually.. |   93   // Error helper for creating errors manually.. | 
|   89   void error(var value) { addError(value); } |   94   void error(var value) { addError(value); } | 
|   90  |   95  | 
|   91   /** Replay the captured events on a sink. */ |   96   /** Replay the captured events on a sink. */ | 
|   92   void replay(EventSink sink) { |   97   void replay(EventSink sink) { | 
|   93     for (int i = 0; i < events.length; i++) { |   98     for (int i = 0; i < events.length; i++) { | 
|   94       events[i].replay(sink); |   99       events[i].replay(sink); | 
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  150     super.addError(error); |  155     super.addError(error); | 
|  151     if (cancelOnError) onDoneSignal.complete(null); |  156     if (cancelOnError) onDoneSignal.complete(null); | 
|  152   } |  157   } | 
|  153  |  158  | 
|  154   void close() { |  159   void close() { | 
|  155     super.close(); |  160     super.close(); | 
|  156     if (onDoneSignal != null) onDoneSignal.complete(null); |  161     if (onDoneSignal != null) onDoneSignal.complete(null); | 
|  157   } |  162   } | 
|  158  |  163  | 
|  159   void pause([Future resumeSignal]) { |  164   void pause([Future resumeSignal]) { | 
 |  165     if (trace) print("Events#$hashCode: pause"); | 
|  160     subscription.pause(resumeSignal); |  166     subscription.pause(resumeSignal); | 
|  161   } |  167   } | 
|  162  |  168  | 
|  163   void resume() { |  169   void resume() { | 
 |  170     if (trace) print("Events#$hashCode: resume"); | 
|  164     subscription.resume(); |  171     subscription.resume(); | 
|  165   } |  172   } | 
|  166  |  173  | 
|  167   void onDone(void action()) { |  174   void onDone(void action()) { | 
 |  175     if (trace) print("Events#$hashCode: onDone"); | 
|  168     onDoneSignal.future.whenComplete(action); |  176     onDoneSignal.future.whenComplete(action); | 
|  169   } |  177   } | 
|  170 } |  178 } | 
| OLD | NEW |