
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
js-event-bus
Advanced tools
Simple Event Bus library built for any JavaScript application.
npm i js-event-bus --save
yarn add js-event-bus
This library was built so you can use it in any JS application like Node.js apps, browser apps etc. The API is always the same.
If you want to use it in your Node.js apps you can import the library like this:
const eventBus = require('js-event-bus')();
If you want to use it in your Browser apps you can import the library like this:
<body>
<div>Put your content here</div>
<script src="/lib/js-event-bus/lib/js-event-bus.min.js"></script>
<script>
const eventBus = new EventBus();
</script>
</body>
eventBus.on('my-event', function () {
console.log('Inside `my-event`');
});
With this code, each time my-event
is emited this function will be executed.
eventBus.once('my-event', function () {
console.log('Inside `my-event`. It\'ll be executed only one time!');
});
With this code, when my-event
is emited this function will be executed. The next triggers of this event won't execute the callback because it is a one time event.
eventBus.exactly(3, 'my-event', function () {
console.log('Inside `my-event`. It\'ll be executed only 3 times!');
});
With this code, when my-event
is emited this function will be executed with a maximum of triggers of 3.
You can emit an event by calling the emit
function. The arguments are the following:
Here are some examples:
eventBus.emit('my-event');
eventBus.emit('my-event', null, 'a', 'b'); // your callback sould be function (a, b) { ... }
eventBus.emit('my-event', new SomeObject, 'a', 'b'); // your callback sould be function (a, b) { ... } and `this` will be set to the context of `SomeObject`
var callbackForMyEvent = function () {
console.log('Inside `my-event`.');
};
eventBus.on('my-event', callbackForMyEvent);
eventBus.emit('my-event');
eventBus.detach('my-event', callbackForMyEvent);
This code will emit the event my-event
and then detach the given callback for this event. So it'll not be executed anymore.
eventBus.detach('my-event');
This code will remove the event my-event
from the event bus.
eventBus.detachAll();
This code will remove the event my-event
from the event bus.
eventBus.on('my-event', function () {
console.log('Inside `my-event`.');
});
eventBus.emit('my-event');
eventBus.die('my-event');
This code will emit the event my-event
and then detach all the callbacks for this event. So any of them won't be executed anymore.
Note that off
is an alias of die
.
MIT
FAQs
Event bus for your Javascript applications
The npm package js-event-bus receives a total of 2,956 weekly downloads. As such, js-event-bus popularity was classified as popular.
We found that js-event-bus 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.