Git Product home page Git Product logo

event.js's Introduction

Event.js - Small event pub/sub library

Event.js tries to plug the gaps in other event pub/sub bind/trigger or whatever-method-you-use libraries. It's extremely simple, but shouldn't do anything you don't expect.

Usage

Let's start by require()ing Event.js and creating an instance:

Vent    = require('EventJS');
MyEvent = new Vent();

Subscribing to an event

To subscribe a callback to an event, simply use subscribe(name, callback):

MyEvent.subscribe('test', myCallback);

If you are using the method of a particular object, then use the bind argument:

MyEvent.subscribe('test', myFoo.myCallback, myFoo);

This will ensure that myFoo.myCallback is bound to myFoo when it is called. Do not use Vent.subscribe('test', myFoo.myCallback.bind(myFoo)), as this causes problems when we try to unsubscribe events.

Unsubscribing from an event

To unsubscribe, you must use exactly the same call signature you use for subscribe, but this time for unsubscribe:

MyEvent.unsubscribe('test', myCallback);

Or, if you subscribed an instance method:

MyEvent.unsubscribe('test', myFoo.myCallback, myFoo);

Publishing events

To notify all the subscribers, use the publish method:

MyEvent.publish('test', 'Hello');

This will call each subscribed callback with a single argument, "Hello". To add more arguments, just put them in the call signature:

MyEvent.publish('test', 'Hello', 'World');

This will call each subscribed callback with two arguments, "Hello" and "World".

Implementing functionality on your own class

Event.js can implement the pub/sub functionality on any other class using the implementOn class method:

Vent.implementOn(MyClass.prototype);

Then you can use it as before:

var myInstance = new MyClass();

myInstance.subscribe(/* ... */);
// ... and so on

Some things this fixes

My primary motivation for writing Event.js was that it fixed some problems I was having in some other libraries. When unsubscribing one instance's callback I often found that this would unsubscribe all the instances' callbacks, or fail to recognise the instance/callback combination, because I had to use callback.bind(obj). The way Event.js does things, by having a separate bind argument, avoids problems with this, which are related to the way .bind() works (it creates a new function, so equality tests always fail).

event.js's People

Watchers

Gideon Farrell avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.