🔨 Lightweight and simple interpretation of popular event management.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.github
dist
src
test/unit
.gitignore
.travis.yml
CHANGELOG.md
README.md
index.d.ts
index.js
package-lock.json
package.json
typings.json
wallaby.js
webpack.config.js

README.md

super-event-emitter

npm version downloads count travis dependencies greenkeeper coveralls

🔨 Lightweight and simple interpretation of popular event management / aggregation

Installation

npm install super-event-emitter

TypeScript Definitions

typings install github:piecioshka/super-event-emitter/typings.json -GD

Usage

Demo #1: Simple literal object:

var foo = EventEmitter.mixin({});

foo.on('test', function () {
    console.log('triggered!');
}, this);

foo.emit('test');

Demo #2: Existed object:

var bar = {}; // Or any other object.
EventEmitter.mixin(bar);

bar.on('test', function () {
    console.log('triggered!');
}, this);

bar.emit('test');

Demo #3: ECMAScript 2015 (ES6) example:

// File: test.js
class Person {
    constructor() {
        EventEmitter.mixin(this);
    }
    
    say(message) {
        this.emit('say', message);
    }
}

// In some cases we have an error:
// SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
// so use `var` statement.
var p1 = new Person();

p1.on('say', function (message) {
    console.log(message);
});

p1.say('cookie');

Try to test this by: node --harmony test.js

API

on( name: string, fn: Function, ctx: Object )

  • name - a string value representing the name of event
  • fn - action which will be called when event will be triggered
  • ctx - object will be context of triggered action

Example:

instance.on('foo', function (payload) {
    console.log(payload, this);
}, instance);

Special behaviour:

  • when name = all (or *) register handler will be fired on any event
instance.on('all', function (name, payload) {
    // will be fired, when emit any of event type
    // - name - event name, in this example will be equal: "something"
    // - payload - data which are sended, in this example will be equal: { foo: 1 }
});

instance.emit('something', { foo: 1 });

once( name: string, fn: Function, ctx: Object )

The same as on but, after triggered event, destroy all listeners

Example:

instance.once('foo', function (payload) {
    console.log(payload, this);
}, instance);

off( name: string, fn: Function )

  • name - a string value representing the name of event
  • fn - action which will be removed from listeners

Example:

instance.off('foo', fooHandler);

emit( name: string, params: Object )

  • name - a string value representing the name of event
  • params - will be passed as first argument in called actions

Example:

instance.emit('foo', { name: 'bar' });

For compatibility with any other APIs I was added some aliases:

  • on => addEventListener, addListener, bind
  • off => removeEventListener, removeListener, unbind
  • emit => dispatchEventListener, dispatchListener, trigger

Article

I was created a blog post (in polish) about this tool: https://piecioshka.pl/blog/2016/01/29/narzedzia-swiata-super-event-emitter-js.html

Unit tests

npm test

Code coverage

Check code coverage with Istanbul:

npm run coverage

License

The MIT License @ 2016