Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

channel-emitter

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

channel-emitter

An event emitter with channel and broadcast support

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

channel-emitter

Codeship Status for CentralPing/channel-emitter Build Status Code Climate for CentralPing/channel-emitter Dependency Status for CentralPing/channel-emitter

Installation

npm i --save channel-emitter

What is ChannelEmitter?

  • A "singleton" EventEmitter - at its core it is a pseudo-singleton (it relies on the node module cache) EventEmitter. Every module within a process that requires ChannelEmitter will recieve the same cached object.
  • A multi-channel emitter - ChannelEmitter supports channels as well as sub-channels. This allows listeners for an event on specific channels as well as emitting up to parent channels and broadcasting down to sub-channels.

Features

  • Provides a "singleton" (a node module cached object).
  • Allows name-spacing of events through "channels".
  • Every channel can have multiple sub-channels.
  • A channel can emit to all events on the channel as well as up to all parent channels.
  • A channel can broadcast to all events on the channel as well as down to all sub-channels.

Examples

Basic Usage

As a singleton
/* a.js */
var channelEmitter = require('channel-emitter');
channelEmitter.on('foo', function () { console.log(arguments); });

/* b.js */
var channelEmitter = require('channel-emitter');
channelEmitter.emit('foo', true, {foo: []}, 123);

/* c.js */
require('./a');
require('./b');
// outputs: { '0': true, '1': { foo: [] }, '2': 123 }
With channels
/* a.js */
var channelEmitter = require('channel-emitter');
channelEmitter.on('foobar', function () { console.log('foobar: ', arguments); });
channelEmitter.on('foo.bar', function () { console.log('foo.bar: ', arguments); });

channelEmitter.emit('foobar', 'hi');
// returns true;
// outputs: foobar: { 0: 'hi' }

channelEmitter.emit('bar', 'hello');
// returns false;
// outputs:

channelEmitter.emit('foo.foobar', 'hi');
// returns true;
// outputs: foobar: { 0: 'hi' }

channelEmitter.emit('foo.bar', 'hello');
// returns true;
// outputs: foo.bar: { 0: 'hello' }

channelEmitter.broadcast('foobar', 'hi');
// returns true;
// outputs: foobar: { 0: 'hi' }

channelEmitter.broadcast('bar', 'hello');
// returns true;
// outputs: foo.bar: { 0: 'hello' }

channelEmitter.broadcast('foo.foobar', 'hi');
// returns false;
// outputs:

channelEmitter.broadcast('foo.bar', 'hello');
// returns true;
// outputs: foo.bar: { 0: 'hello' }

API Reference

Example

var channel_emitter = require('channel-emitter');

channel-emitter~addListener(eventName, listener) ⇒ ChannelEmitter

Wrapper for the EventEmitter.addListener method that will auto-add channels if the specified delimiter is used in the name.

Kind: inner method of channel-emitter

ParamTypeDescription
eventNamestringthe name for the event
listenerfunctionthe listener for the event

channel-emitter~removeListener(eventName, listener) ⇒ ChannelEmitter

Wrapper for the EventEmitter.removeListener method that will remove events from a specified channl if the specified delimiter is used in the name.

Kind: inner method of channel-emitter

ParamTypeDescription
eventNamestringthe name for the event
listenerfunctionthe listener for the event

channel-emitter~removeAllListeners([eventName]) ⇒ ChannelEmitter

Wrapper for the EventEmitter.removeAllListeners method that will remove if the specified delimiter is used in the name.

Kind: inner method of channel-emitter

ParamTypeDescription
[eventName]stringthe name for the event

channel-emitter~listenerCount(eventName) ⇒ ChannelEmitter

Wrapper for the EventEmitter.listenerCount method that will return the listener count on a channel (including name-spaced events).

Kind: inner method of channel-emitter

ParamTypeDescription
eventNamestringthe name for the event

channel-emitter~listeners(eventName) ⇒ ChannelEmitter

Wrapper for the EventEmitter.listeners method that will return the listeners on a channel (including name-spaced events).

Kind: inner method of channel-emitter

ParamTypeDescription
eventNamestringthe name for the event

channel-emitter~on(eventName, listener) ⇒ ChannelEmitter

Wrapper for the EventEmitter.on method that will auto-add channels if the specified delimiter is used in the name.

Kind: inner method of channel-emitter

ParamTypeDescription
eventNamestringthe name for the event
listenerfunctionthe listener for the event

channel-emitter~addChannel(channelName) ⇒ ChannelEmitter

Adds a sub-channel to the current channel.

Kind: inner method of channel-emitter

ParamTypeDescription
channelNamestringthe name for the channel

channel-emitter~removeChannel(channelName) ⇒ ChannelEmitter

Removes the sub-channel from the current channel.

Kind: inner method of channel-emitter

ParamTypeDescription
channelNamestringthe name for the channel

channel-emitter~emit(eventName, [...args]) ⇒ Boolean

EventEmitter wrapper that emits an event to siblings and direct ancestor channels.

Kind: inner method of channel-emitter

ParamTypeDescription
eventNamestringthe name for the registered event
[...args]*arguments to emit to the event

channel-emitter~broadcast(eventName, [...args]) ⇒ Boolean

Broadcasts an event to siblings and descendent channels.

Kind: inner method of channel-emitter

ParamTypeDescription
eventNamestringthe name for the registered event
[...args]*arguments to broadcast to the event

License

Apache 2.0

Keywords

FAQs

Package last updated on 19 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc