Lightweight library to create custom events and trigger and subscribe them from everywhere. Works standalone or with other frameworks like react.js or vue.js
Sometimes you need a simple implementation to create global channels and subscribe them from other pieces of code, components or stuff like that. With ez-channel it is easy ;-)
If you want to define, trigger and process events in different files, this library can help you. You only need to include ez-channel in your files where you want to work with your custom events.
Package is available here: https://www.npmjs.com/package/ez-channel
npm i ez-channel
Just include it into your web or server project
node
const Channel = require('ez-channel');
web
import Channel from 'ez-channel';
or you can include the transpiled script from dist folder.
A channel is that object that manage all events and subscriptions for you. To create a new channel, just do it like follow.
Channel.create("myAwesomeChannel");
Subscribe gives you all the functions that you need to trigger and listen for events. See example below.
const channel = Channel.subscribe();
If you want to create an event, uhm let´s say "youAreAwesome" and you want listen to that, just do it like follow.
channel.on("myAwesomeChannel", "youAreAwesome", function() {
console.log("someone says your are awesome");
});
If you want to dispatch an events, just simple do it like follow.
channel.send("myAwesomeChannel", "youAreAwesome");
Of course it is possible the send and receive data by "on" and "send". See example below:
channel.send("myAwesomeChannel", "youAreAwesome",{isAwesome: true});
channel.on("myAwesomeChannel", "youAreAwesome", function(obj) {
if(obj.isAwesome) {
console.log("you are awesome");
}
});
Maybe it make sometimes sense to remove a channel. You can do it like follow.
channel.remove("myAwesomeChannel");
Apache 2.0
Happy using and keep coding =)