Skip to content

behemehal/event_listener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventListener

License Pub Build Status

NodeJS like Event Listener library for dart!

Usage

On event

import 'package:event_listener/event_listener.dart';

void main() {
  var eventListener = new EventListener();

  eventListener.on('log', (String message) {
    print("A log: " + message);
  });

  eventListener.emit('log', 'from me');
}

Once event fires one time

import 'package:event_listener/event_listener.dart';

void main() {
  var eventListener = new EventListener();

  //This event created by package called every time when you assign new listener to event
  eventListener.on('newListener', (MapEntry<String, Event> deletedEvent) {
    print("New event name: " + deletedEvent.key);
    print("New event caller: " + deletedEvent.value.caller);
  });
  /*
    New event name: log
    new event caller: closure (String message) =>
  */

  eventListener.once('log', (String message) {
    print("A last log: " + message);
    //A last log: from me
  });
  eventListener.emit('log', 'from me');
}

Remove listener

import 'package:event_listener/event_listener.dart';

void main() {
  var eventListener = new EventListener();

  //This event created by package called every time when you remove a listener from event
  eventListener.on('removeListener', (MapEntry<String, Event> deletedEvent) {
    print("Deleted event name: " + deletedEvent.key);
    print("Deleted event caller: " + deletedEvent.value.caller);
  });
  /*
    Deleted event name: log
    Deleted event caller: closure (String message) =>
  */

  var logMe = (message) {
    print("A last log: " + message);
  }
  //A last log: from me

  eventListener.once('log', logMe);
  eventListener.emit('log', 'from me');
  eventListener.removeEventListener('log', logMe);
}

Remove all listeners

import 'package:event_listener/event_listener.dart';

void main() {
  var eventListener = new EventListener();

  var logMe = (String message) {
    print("A last log: " + message);
  }

  eventListener.once('log', logMe);
  eventListener.on('log', logMe);
  eventListener.emit('log', 'from me');
  eventListener.removeAllListeners('log');
}

Contributing

This package made close as NodeJS's Event Listener module, feel free to modify package as long as it looks familiar to Node package