eventus 1.0.0 copy "eventus: ^1.0.0" to clipboard
eventus: ^1.0.0 copied to clipboard

Dart 1 only

Eventus is a professional implementation of EventEmitter for Dart.

example/eventus.dart

import 'package:eventus/eventus.dart';

/**
 * To use the Eventus you only need to extend the
 * Eventus class.
 */
class Test extends Eventus {

  /**
   * Dummy handler function.
   *
   * @param String msg    Message to be processed.
   */
  void handleMsg(String msg) {
    print("Class Function");
  }

}

main() {
  Test obj = new Test();

  // add a handler to `message` event
  obj.on('message', (String msg) {
    print("> ${msg}");
  });

  // add another handler. when we use named functions we can
  // remove the handler with the `removeListener` method
  obj.on('message', obj.handleMsg);

  // add a once event handler. this handler is removed in his
  // first execution
  obj.once('message', (String msg) {
    print("Once > ${msg}");
  });

  // just another handler in a different event
  obj.on('login', (String username, String password) {
    print("${username} - ${password}");
  });

  // call a event who not exists. this call not pass any arguments
  // to the handlers
  obj.emit('notExists');

  // should call the three handlers
  obj.emit('message', 'M#1');

  // remove the class `handleMsg` handler associated to the
  // `message` event
  obj.removeListener('message', obj.handleMsg);

  // the once handler was been removed at this time
  obj.emit('message', 'M#2');

  // remove all listeners associated to the message event
  obj.removeAllListeners('message');

  // no one it's listening the `message` event
  obj.emit('message', 'M#3');

  // should raise an exception. the handler not have the
  // correct number of arguments. two correct that the handler
  // can change his signature to `(String username, [String password])`
  try {
    obj.emit('login', 'asd');
  } on NoSuchMethodError catch (_) {}

  // clear all class listeners
  obj.clearListeners();

  // every listeners was been removed
  obj.emit('login', 'user', 'pass');
}
0
likes
20
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

Eventus is a professional implementation of EventEmitter for Dart.

License

BSD-3-Clause (license)

Dependencies

dict

More

Packages that depend on eventus