Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Implementation of channels (aka events, pub/sub, dispatcher) inspired and
compatible with Chrome extensions events API.
Additionally, there are some extra features (e.g. passing context
with listener) and utility classes
that makes life easier.
npm i chnl --save
Channel is class that can attach/detach listeners and dispatch data to them.
This allows to make more independent modules that just produce events in outer world.
API:
Example:
module A
import Channel from 'chnl';
// create channel
exports.onChanged = new Channel();
// dispatch event (and don't care if there module B and what it will do with event)
exports.onChanged.dispatch(data);
// dispatch event asynchronously via setTimeout(..., 0);
exports.onChanged.dispatchAsync(data);
module B
import moduleA from './moduleA';
// subscribe on channel
moduleA.onChanged.addListener(data => {
console.log('moduleA.onChanged', data);
});
// subscribe once
moduleA.onChanged.addOnceListener(data => {
console.log('moduleA.onChanged once', data);
});
// mute channel (optionally you can accumulate events to dispatch them after unmute)
moduleA.onChanged.mute({accumulate: true});
// unmute channel
moduleA.onChanged.unmute();
EventEmitter is basically a group of channels with common api.
The main difference from single channel is that each method takes additional event
argument.
Not all methods of original nodejs EventEmitter
are supported but most needed:
Example:
import Channel from 'chnl';
// use as standalone object
const emitter = new Channel.EventEmitter();
emitter.on('event', data => console.log(data));
emitter.emit('event', 'hello world!'); // output 'hello world!'
// use as parent for some class
class MyClass extends Channel.EventEmitter {
someMethod() {
this.emit('event', 'hello world!')
}
}
const myClass = new MyClass();
myClass.on('event', data => console.log(data));
myClass.someMethod(); // output 'hello world!'
Subscription is utility class allowing dynamically attach/detach batch of listeners to event channels.
You pass array of {channel: ..., event: ..., listener: ...}
to constructor and then manage it via two methods:
Example:
import Channel from 'chnl';
this._subscription = new Channel.Subscription([
{
channel: chrome.tabs.onUpdated,
listener: this._onTabUpdated.bind(this)
},
{
channel: document.getElementById('button'),
event: 'click',
listener: this._onButtonClick.bind(this)
}
]);
this._subscription.on(); // now listeners are attached
this._subscription.off(); // now listeners are detached
Extends Subscription class and can be used for attaching/detaching listeners in ReactComponent callbacks componentDidMount/componentWillUnmount
Example via Subscription class:
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();
}
}
Example via ReactSubscription class:
class Button extends React.Component {
constructor() {
super();
new Channel.ReactSubscription(this, [
{channel: onNewData, listener: this.handleNewData.bind(this)}
]);
}
}
FAQs
Implementation of event channels compatible with Chrome extensions events API
The npm package chnl receives a total of 0 weekly downloads. As such, chnl popularity was classified as not popular.
We found that chnl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.