megaphone

Fast simple event emitter with event proxy support

npm install megaphone
2 downloads in the last week
8 downloads in the last month

Megaphone

Megaphone is a fast and simple event emitter.

$ npm install megaphone

Download (Unminified)

API

emit(name, [...args])

Execute listeners that match the provided name with the provided args.

on(name, fn, [unshift])

Add a listener. If unshift is true then the listener will be shoved onto the start of the listeners array rather than the end.

once(name, fn)

Add a listener and remove it after being fired once.

times(name, times, fn)

Add a listener and remove it after being fired after times.

many(names, fn)

Add a listener to multiple names where names is an array.

off([name], fn)

Remove listener.

onAny(fn)

Add a listener that will be fired on any event.

offAny([fn])

Remove onAny listener.

If no function is passed then all onAny functions are removed.

proxy(emitter, [namespace])

Proxy all emits from self to the passed emitter. If a namespace is passed then all the proxied events with be appended with it.

proxyOff(emitter, [namespace])

Remove a proxy.

listeners(name)

Return a list of listeners that match the provided name.

hasListeners(name)

Check if any listeners are set with the provided name.

Examples

Basic

var foo = new Megaphone;

foo.on("bar", function(){

});

foo.on("foo", function(str, arr, obj, num){

});

foo.many(["foo", "bar"], function(){

});

foo.emit("bar");
foo.emit("foo", "", [], {}, 5);

Any

var foo = new Megaphone;

foo.onAny(function(name, num){
    // caught event
});

foo.emit("bar", 5);

foo.offAny();

Proxies

var foo = new Megaphone;
var bar = new Megaphone;

foo.proxy(bar, "bar");
foo.on("bar namedspaced", function(){
    // proxied from `bar`
});

bar.proxy(foo);
bar.on("no namespace", function(){
    // proxied from foo
});

bar.emit("namedspaced");
foo.emit("no namespace");

foo.proxyOff(bar);
bar.proxyOff(foo);

License

Copyright (c) 2013 Sebastian McKenzie

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
npm loves you