Channel for events of particular type. Allows attach/detach listeners and dispatch event data.

new Channel(name: String?)
Parameters
name (String?)
Example
import Channel from 'chnl';

// create channel
const onClick = new Channel();
// listen
onClick.addListener(data => console.log(data));
// dispatch data
onClick.dispatch(data);
Static Members
▸ isValidListener(listener)
Instance Members
▸ onListenerAdded
▸ onFirstListenerAdded
▸ onListenerRemoved
▸ onLastListenerRemoved
▸ addListener(callback, context?)
▸ addOnceListener(callback, context?)
▸ removeListener(callback, context?)
▸ removeAllListeners()
▸ hasListener(callback, context?)
▸ hasListeners()
▸ dispatch(args)
▸ dispatchAsync(args)
▸ mute(options = {})
▸ unmute()

Event emitter similar to Node.js EventEmitter. The main difference from single channel is that each method takes additional event argument.

new EventEmitter()
Example
import Channel from 'chnl';

// create emitter
const emitter = new Channel.EventEmitter();
// listen 'myEvent'
emitter.on('myEvent', data => console.log(data));
// emit 'myEvent'
emitter.emit('myEvent', 'hello world!');
Instance Members
▸ addListener(event, callback, context?)
▸ on(event, callback, context?)
▸ addOnceListener(event, callback, context?)
▸ once(event, callback, context?)
▸ removeListener(event, callback, context?)
▸ off(event, callback, context?)
▸ hasListener(event, callback, context?)
▸ has(event, callback, context?)
▸ hasListeners(event)
▸ dispatch(event, args)
▸ emit(event, args)

Utility class allowing dynamically attach/detach batch of listeners to event channels.

new Subscription(items: Array<{channel, event, listener}>)
Parameters
items (Array<{channel, event, listener}>)
Example
import Channel from 'chnl';
const subscription = new Channel.Subscription([
  {
    channel: chrome.tabs.onUpdated,
    listener: this._onTabUpdated.bind(this)
  }
]);

// attach listeners
subscription.on();
// detach listeners
subscription.off();
Instance Members
▸ on()
▸ off()

ReactSubscription

src/react-subscription.js

Utility class that extends Subscription for using in ReactComponent - automatically attach/detach listeners in componentDidMount / componentWillUnmount.

new ReactSubscription(component: ReactComponent, items: Array<{channel, event, listener}>)

Extends Subscription

Parameters
component (ReactComponent)
items (Array<{channel, event, listener}>)
Example
class Button extends React.Component {
  constructor() {
    super();
    new Channel.ReactSubscription(this, [
      {channel: onNewData, listener: this.handleNewData.bind(this)}
    ]);
  }
}

// actually equals to (but with more boilerplate code):
class Button extends React.Component {
  constructor() {
    super();
    this.subscription = new Channel.Subscription([
      {channel: onNewData, listener: this.handleNewData.bind(this)}
    ]);
  }
  componentDidMount() {
    this.subscription.on();
  }
  componentWillUnmount() {
    this.subscription.off();
  }
}