Skip to content

acvos/simple-event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-event

Simple event implementation: dispatch payload to one or many listeners

Motivation

This is an essential building block for any reactive-style library or application.

Features

  • Subscribe and unsubscribe listeners
  • Optionally send any payload when emitting event
  • Emit promises: the event will be fired off automatically when the promise resolves

Installation

npm install simple-event

Usage

var simpleEvent = require('simple-event');

// Create event
var myEvent = simpleEvent();

// Subscribe a function
myEvent.subscribe(handleSimpleEvent);
myEvent.subscribe(function (data1, data2, data3) {
    .......
});

// Subscribe function in some context
myEvent.subscribe(function (message) {
    console.log(this.controllerName, message);
}, myController);

// Emit event
myEvent.emit();

// Emit some message
myEvent.emit(message, someExtraData);

// Emit promise: the event will be fired when the promise is resolved
var myData = $http.get('http://blah.com/data');
myEvent.emit(myData);

// Unsubscribe
myEvent.unsubscribe(handleSimpleEvent);

About

Simple event implementation: dispatch payload to one or many listeners

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published