1.2.0
Channel for events of particular type. Allows attach/detach listeners and dispatch event data.
(String?)
import Channel from 'chnl';
// create channel
const onClick = new Channel();
// listen
onClick.addListener(data => console.log(data));
// dispatch data
onClick.dispatch(data);
Triggers when first listener is added to channel.
Channel
:
Triggers when listener is removed from channel.
Channel
:
Triggers when last listener is removed from channel.
Channel
:
Remove all listeners from channel.
Call all listeners with specified params.
(...any)
Call all listeners with specified params asynchronously.
(...any)
Unmute channel.
Event emitter similar to Node.js EventEmitter.
The main difference from single channel is that each method takes additional event
argument.
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!');
Utility class allowing dynamically attach/detach batch of listeners to event channels.
(Array<{channel, event, listener}>)
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();
Utility class that extends Subscription for using in ReactComponent - automatically attach/detach listeners
in componentDidMount
/ componentWillUnmount
.
Extends Subscription
(ReactComponent)
(Array<{channel, event, listener}>)
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();
}
}