EventEmitter class

EventEmitter Class - an asynchronous, broadcastStream-based Class that allows multiple Listeners on each instance, providing notification to each Listener when emit is called on an Event. Each Listener has a coinciding EventCallback Function that is called each time it received notification of an Event being emitted.

Example provides two instances of EventEmitter - evt, & evt_2:

var evt = EventEmitter(); /// no optional args, maxListeners set to 50
var evt_2 = EventEmitter(maxListeners:10); /// maxListeners set to 10
  1. on method provides the registration/subscription aspect of the EventEmitter Class - explained further in the documetation for that method below.
  2. emit method provides a means of publishing the event so that any Listeners registered by on can process the event and decide if it should run it's EventCallback - explained further in the documetation for that method below.
  3. removeListener method removes the specified EventCallback from the _active_events Map, and puts it into the the _removed_events Map - thereby preventing this specific EventCallback being called by this specific Event.
  4. removeListeners method removes multiple EventCallback's, specified via a Set argument, from the _active_events Map, and puts the specified removed EventCallback's into the _removed_events Map - thereby preventing these specific EventCallback's from being called by this specific Event.
  5. removeAllListeners method removes ALL EventCallback's from a given Event.
  6. destroy method closes this instance's Stream and prevents all Event 's that were assigned to this Stream from publishing, and listening, any further, and prevents new subscribers from being added as well. For all intents and purposes, this instance is no longer usable, and therefore destroyed.

Example EventEmitter flow:

void test ([data]){ // no user-defined param, and `data` is not used - but it could be
  print('Hello World!');
}

void test_2 ([counter,data]){ // a single user-defined param
  print('$data has been called $counter times');
}

void main() async {
  var evt = EventEmitter();

  /// no user-defined args, as only data was provided in the function
      definition
  await evt.on('test', test);

  /// one user-defined arg, for the [counter] param. This should be
  /// an array with parameters you defined - everything except for data
  await evt.on('test_2', test_2, [5]);

  await evt.emit('test');

  /// prints: Hello world.
  await evt.emit('test');

  /// prints: Hello world.
  await evt.removeListener('test', test);

  /// returns nothing
  await evt.emit('test');

  /// returns nothing
  print('test Listener has been removed - 3 emits were called, but only 2 were executed because of the .removeListener()');

  await evt.emit('test_2');

  /// prints: test_2 has been called 5 times.
  await evt.emit('test_2');

  /// prints: test_2 has been called 5 times.
  await evt.emit('test_2');

  /// prints: test_2 has been called 5 times.
  await evt.removeListener('test_2', test_2);

  /// returns nothing
  await evt.emit('test_2');

  /// returns nothing
  print('test_2 Listener has been removed - 4 emits were called, but only 3 were executed because of the .removeListener()');

}

Constructors

EventEmitter({dynamic maxListeners: 50})
Class Constructor - automatically sets _maxListeners to 50 via an optional named parameter

Properties

eventCount ↔ int
get _eventCount
read / write
listenerCount ↔ int
get _listenerCount
read / write
maxListeners ↔ int
get _maxListeners
read / write
hashCode → int
The hash code for this object.
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

destroy() → void
destroy closes the Stream and prevents any further use of this instance of EventEmitter.
emit(String eventId) → void
emit adds an Event to the Stream, notifying all listeners of the action.
on(String eventId, EventCallback callback, [List args, void onError(Error error, [StackTrace stacktrace]), void onDone(), bool cancelOnError = true]) → void
on registers and subscribes new listeners and their EventCallback's for future use. [...]
removeAllListeners(String eventId) → void
removeListener, but removes all callback's instead of specified callbacks
removeListener(String eventId, Function callback) → void
removeListener removes a single, specified callback from this specific eventId.
removeListeners(String eventId, Set<EventCallback> callbacks) → void
removeListener, but will remove a Set of specified callbacks
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator.
inherited