Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@knighttower/event-bus
Advanced tools
Refactoring and Updates:
---- 2024 ----
---- 2023 ----
Remove some unnecessary methods to make it SOLID
Remove different file language versions to unify the code into one and ease maintenance
Fixed and made the code JsDocs and Ts compliant
Fixed minor code issues
Added correct distribution files to modules and browser
Added Vitest unit test (passed)
Added terser, webpack (for browser window export) and ts (for module export)
Removed examples folder, the examples are now in the JsDocs comments for Docs creation
Bump the version to Major since there was a lot of refactoring and changes in the Class API
"emit" method now receives the 'context' as part of the 'args' only if set since it is not frequently used and allows to pass the data without the need to set "null" as the context, ex:
Fixes to the wild card event match. It now accepts correct wild card in the "on" or "emit" method:
(this is an updated and improved fork of js-event-bus by [bcerati])
npm i @knighttower/event-bus
yarn add @knighttower/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:
import { eventBus } from '@knighttower/event-bus';
//Tip: in node JS you can use the global instance=> eventBus().global();
If you want to use it in your Browser apps you can import the library (browser or iife) like this:
<script src=" https://cdn.jsdelivr.net/npm/@knighttower/event-bus@latest/dist/browser/eventBus.min.js "></script>
<script>
// global instance
eventBus.default().global();
// then you can use it
window.eventBus.on('my-event', function () {
console.log('Inside `my-event`');
});
// or if you want to use it with scope as
const ev = eventBus.default();
// then you can use it
ev.on('my-event', function () {
console.log('Inside `my-event`');
});
</script>
eventBus.on('my-event', function () {
console.log('Inside `my-event`');
});
With this code, each time my-event
is emitted 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 emitted 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 emitted this function will be executed with a maximum of triggers of 3.
You can use wildcards to register listeners using a specific pattern.
eventBus.on('my-event.*', function () {
console.log('Inside `my-event.*`');
});
The callback will be executed with the events like my-event.x
.
my-event.x
will trigger the callback ;my-event.y
will trigger the callback ;my-event
will not trigger the callback ;my-event.x.y
will not trigger the callback ;You can also use multiple wildcards to register listeners using a specific pattern.
eventBus.on('my-event.*.name.**', function () {
console.log('my-event.*.name.**`');
});
The callback will be executed with the events like my-event.a.name.b.c
.
my-event.a.name.b.c
will trigger the callback ;my-event.a.name.b
will trigger the callback ;my-event.name.b
will not trigger the callback ;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', 'a', 'b', {__context: someInstance}); // your callback sould be function (a, b) { ... }
eventBus.emit('my-event', 'a', 'b', 'c', {__context: new SomeObject()}); // your callback sould be function (a, b) { ... } and `this` will be set to the context of `SomeObject`. Order is not important.
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.on('my-event', function () {
console.log('Inside `my-event`.');
});
eventBus.emit('my-event');
eventBus.off('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.
MIT
FAQs
Event bus for your Javascript applications
We found that @knighttower/event-bus demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.