EventChannel class

Creates channel that will subscribe to an event source using the subscribe method. Incoming events from the event source will be queued in the channel until interested takers are registered.

To notify the channel that the event source has terminated, you can notify the provided subscriber with an End

Example

In the following example we create an event channel that will subscribe to a Timer.periodic

 EventChannel countdown(int secs) {
   return EventChannel(subscribe: (emitter) {
     var v = secs;
     var timer = Timer.periodic(Duration(seconds: 1), (timer) {
       v--;
       if (v > 0) {
         emitter(v);
       } else {
         emitter(End);
       } // this causes the channel to close
     });

     // The subscriber must return an unsubscribe function
     return () => timer.cancel();
   });
 }

 saga() sync* {
   var value = 10;
   var channel = Result<EventChannel>();

   yield Call(countdown, args: [value], result: channel);

   yield Try(() sync* {
     while (true) {
       // Take(pattern:End) will cause the saga to terminate
       // by jumping to the finally block
       var seconds = Result();
       yield Take(channel: channel.value, result: seconds);
       print('countdown: ${seconds.value}');
     }
   }, Finally: () sync* {
     print('countdown terminated');
   });
 }

Output

 countdown: 9
 countdown: 8
 countdown: 7
 countdown: 6
 countdown: 5
 countdown: 4
 countdown: 3
 countdown: 2
 countdown: 1
 countdown terminated

Constructors

EventChannel(Subscribe subscribe, {Buffer? buffer})
Creates an instance of an EventChannel

Properties

hashCode int
The hash code for this object.
no setterinherited
onClose Callback?
Invoked before closing the channel. Then all the awating takers will be invoked with End
getter/setter pairinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

close() → void
Closes the channel which means no more puts will be allowed. All pending takers will be invoked with End.
inherited
flush(TakeCallback<List> callback) → void
Used to extract all buffered messages from the channel. The flush is resolved using the following rules
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put(dynamic message) → void
Used to put message on the buffer. The put will be handled using the following rules
inherited
take(TakeCallback callback, [PatternMatcher? matcher]) → void
Used to register a taker. The take is resolved using the following rules
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited