ckyambitny / super-event-emitter

:hammer: Super small (2KB) and simple interpretation of popular event management.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

super-event-emitter (npm)

npm version Travis Coverage Status

Super small (2KB) and simple interpretation of popular event management / aggregation.

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

Install

$ 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

Unit tests

How to run unit test (written in Jasmine):

$ npm test

Code coverage

Use Istanbul to get code coverage ratio.

$ npm run coverage

License

The MIT License @ 2016

About

:hammer: Super small (2KB) and simple interpretation of popular event management.


Languages

Language:JavaScript 100.0%