Giter Club home page Giter Club logo

eldargab-hooks-emitter's Introduction

This repository is a mirror of the component module eldargab/hooks-emitter. It has been modified to work with NPM+Browserify. You can install it using the command npm install npmcomponent/eldargab-hooks-emitter. Please do not open issues or send pull requests against this repo. If you have issues with this repo, report it to npmcomponent.

hooks-emitter

What's this and why?

Often event emitters are used not for sending signals to the outside world but as a general purpose hooking API. The difference is that hooks are known in advance, they are not specific to concrete instance. The natural thing to do for such cases is:

Widget.prototype.on('show', function() {
  // do something
})

Obviously traditional event emitters do not allow that. They force you to do all subscriptions in constructor. That is

  • slow
  • inflexible (I think Super.call(this) is a smell)

This project implements emitter in a different way. It allows you to add subscriptions on prototype and generally you should not think about clobbering or Emitter.call(this) calls. At the moment of writing it can be used as a drop in replacement for component/emitter.

Examples

var Emitter = require('hooks-emitter')
var calls = []

var proto = new Emitter

proto.on('foo', function () {
  calls.push('proto')
})

var instance = Object.create(proto)

instance.on('foo', function () {
  calls.push('emitter')
})

instance.emit('foo')

calls.should.eql(['proto', 'emitter'])

proto.emit('foo')

calls.should.eql(['proto', 'emitter', 'proto'])

Addition of handlers to prototype when child instance was already created is not supported. For such case behaviour is undefined.

var proto = new Emitter
var instance = Object.create(proto)
proto.on('foo', listener) // that's a bit funky
instance.emit('foo')

Installation

Via npm

npm install hooks-emitter

Via component

component install eldargab/hooks-emitter

API

Emitter(obj)

As an Emitter instance:

var Emitter = require('emitter');
var emitter = new Emitter;
emitter.emit('something');

As a mixin:

var Emitter = require('emitter');
var user = { name: 'tobi' };
Emitter(user);

user.emit('im a user');

Emitter#on(event, fn)

Register an event handler fn.

Emitter#once(event, fn)

Register a single-shot event handler fn, removed immediately after it is invoked the first time.

Emitter#off(event, fn)

Remove event handler fn, or pass only the event name to remove all handlers for event.

Emitter#emit(event, ...)

Emit an event with variable option args.

Emitter#listeners(event)

Return an array of callbacks, or an empty array.

Emitter#hasListeners(event)

Check if this emitter has event handlers.

License

MIT

eldargab-hooks-emitter's People

Contributors

airportyh avatar eldargab avatar

Watchers

 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.