No description
Find a file
2016-10-03 15:51:47 -07:00
examples es6 update, examples and chaining 2016-09-30 03:58:44 -07:00
bower.json es6 update, examples and chaining 2016-09-30 03:58:44 -07:00
es5.js es6 update, examples and chaining 2016-09-30 03:58:44 -07:00
event-pubsub-browser-es5.js es6 update, examples and chaining 2016-09-30 03:58:44 -07:00
event-pubsub-browser.js es6 update, examples and chaining 2016-09-30 03:58:44 -07:00
event-pubsub.js es6 update, examples and chaining 2016-09-30 03:58:44 -07:00
LICENSE Initial commit 2014-02-18 13:48:27 -08:00
package.json rev docs for ES6/ES5 selection 2016-10-03 15:51:47 -07:00
README.md documented how to auto choose ES5 and ES6 2016-10-03 15:50:52 -07:00

Event PubSub

npm info :
event-pubsub npm version total npm downloads for event-pubsub monthly npm downloads for event-pubsub

GitHub info :
event-pubsub GitHub Release GitHub license event-pubsub license open issues for event-pubsub on GitHub

Super light and fast Extensible PubSub events and EventEmitters for Node and the browser with support for ES6 by default, and ES5 versions for older verions of node and older IE/Safari versions.

Easy for any developer level. No frills, just high speed events following the publisher subscriber pattern!

Pretty GitHub.io site

See NPM stats for event-pubsub

EXAMPLE FILES

  1. Node Event PubSub Examples
  2. Browser Event PubSub Examples

Node Install
npm i --save event-pubsub
By default the ES6 version will be used. you can use the es5 version for older versions of node by requiring event-pubsub/es5.js.

Browser Install
see browser examples above or below


<script src='path/to/event-pubsub-browser.js'></script>
<!-- OR ES5 for older browser support
    <script src='path/to/event-pubsub-browser-es5.js'></script>
-->

Methods

Method Arguments Description
subscribe type (string), handler(function) will bind the handler function to the the type event. Just like addEventListener in the browser
on same as above same as above
unSubscribe type (string), handler(function or *) will unbind the handler function from the the type event. If the handler is *, all handlers for the event type will be removed. Just like removeEventListener in the browser, but also can remove all event handlers for the type.
off same as above same as above
publish type (string), ...data arguments will call all handler functions bound to the event type and pass all ...data arguments to those handlers
emit same as above same as above
trigger same as above same as above

While publish, subscribe, and unSubscribe are the proper method names for the publisher/subscriber model, we have included on, off, and emit as well because these are the most common event method names, and shorter. We have also kept the trigger method as an alias for publish and emit for backwards compatability with earlier versions of event-pubsub.

The * type

The * type is a special event type that will be triggered by any publish or emit the handlers for these should expect the first argument to be the type and all arguments after to be data arguments.

Basic Examples

NOTE - the only difference between node and browser code is how the events variable is created

  • node const events = new Events
  • browser const events = new window.EventPubSub;

Automatically choosing ES6 || ES5


//use es6 by default
let Events = require('event-pubsub');
//if not using atleast node v5.0.0
if(process.version[1]<5){
    //downgrade to ES5
    Events = require('event-pubsub/es5');
}

Node


const Events = new require('event-pubsub');
const events=new Events;

events.on(
    'hello',
    function(data){
        console.log('hello event recieved ', data);
        events.emit(
            'world',
            {
                type:'myObject',
                data:{
                    x:'YAY, Objects!'
                }
            }
        )
    }
);

events.on(
    'world',
    function(data){
        console.log('World event got',data);
        events.off('*','*');
        console.log('Removed all events');
    }
);

events.emit(
    'hello',
    'world'
);



Basic Chaining


events.on(
    'hello',
    someFunction
).on(
    'goodbye',
    anotherFunction
).emit(
    'hello',
    'world'
);

events.emit(
    'goodbye',
    'complexity'
).off(
    'hello',
    '*'
);

Browser

HTML


    <!DOCTYPE html>
    <html>
        <head>
            <title>PubSub Example</title>
            <script src='../../event-pubsub-browser.js'></script>
            <!-- OR ES5 for older browser support
                <script src='../../event-pubsub-browser-es5.js'></script>
            -->
            <script src='yourAmazingCode.js'></script>
        </head>
        <body>
            ...
        </body>
    </html>


Inside Your Amazing Code

var events = new window.EventPubSub();

events.on(
    'hello',
    function(data){
        console.log('hello event recieved ', data);
        events.emit(
            'world',
            {
                type:'myObject',
                data:{
                    x:'YAY, Objects!'
                }
            }
        )
    }
);

 events.emit(
     'hello',
     'world'
 );

 events.emit(
     'hello',
     'again','and again'
 );


Basic Event Emitter and/or Extending Event PubSub


const Events = require('event-pubsub');

class Book extends Events{
    constructor(){
        super();
        //now Book has .on, .off, and .emit

        this.words=[];
    }

    add(...words){
        this.words.push(...words);
        this.emit(
            'added',
            ...words
        );
    }

    read(){
        this.emit(
            'reading'
        );
        console.log(this.words.join(' '));
    }
}

const book=new Book;

book.on(
    'added',
    function(...words){
        console.log('words added : ',words);
        this.read();
    }
);

book.add(
    'once','upon','a','time','in','a','cubicle'
);


ES5 extention example

const Events = require('event-pubsub/es5.js');

function Book(){
    //extend happens below
    Object.assign(this,new Events);
    //now Book has .on, .off, and .emit

    this.words=[];
    this.add=add;
    this.erase=erase;
    this.read=read;

    function add(){
        arguments.slice=Array.prototype.slice;

        this.words=this.words.concat(
            arguments.slice()
        );
        this.emit(
            'added',
            arguments.slice()
        );
    }

    function read(){
        this.emit(
            'reading'
        );
        console.log(this.words.join(' '));
    }

    return this;
};

const book=new Book;

book.on(
    'added',
    function(...words){
        console.log('words added : ',words);
        this.read();
    }
);

book.add(
    'once','upon','a','time','in','a','cubicle'
);