
Product
Introducing Pull Request Stories to Help Security Teams Track Supply Chain Risks
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
simple-event-bus
Advanced tools
This project implements a basic but configurable event bus.
// In Node.js
const EventBus = require('simple-event-bus');
// For the browser, using a bundler
import EventBus from 'simple-event-bus';
const eventBus = new EventBus();
// subscribe to events
const removeHandler = eventBus.on('my-event', (...args) => {
console.log(...args);
});
// trigger events
eventBus.emit('my-event', 'Hello', 'world', '!');
// => Hello world !
// unsubscribe when you are done
removeHandler();
Live Demo and its source code.
new EventBus([options: Object])
Returns the event bus instance, that holds the handlers and is able to dispatch events
eventBus.on(action: string, handler: Function, [handlerId: number]): Function
Registers the handler for the given action. Optionally, a handlerId can be passed, that will be used when logging errors. It returns a function that removes the handler when called.
eventBus.emit(action: string, ...parameters: any): number
Emits an event, dispatching it to all registered handlers. It does not do anything if no handlers have been registered on that event.
eventBus.handlersCount(action: string)
Returns the number of handlers currently subscribed to the action.
[options = {}]
options.logger
Type: Object
Default: console
The only mandatory method for now is error
, but some additional loggings will most likely be added (log
, info
, debug
, warn
);
options.broker
Type: Function
Default: (action, ...payload) => payload
A function to customize the interface between events and handlers, giving the ability for example to standardize all the messages transmitted to the handlers into a normalized format. By default, the broker just propagates the arguments that have been emitted.
The node entry point ("main": "index.js"
in the package.json
) requires Node >= 6.5 (see ES6 compatibility table for class). The browser entry point ("browser": "browser.js" in the package.json
) exposes some code bundled and transpiled into ES5 for compatibility with older browsers, bundlers, minifiers, ...
More logging: Currently, we only log errors. We could log more information, to help with troubleshooting.
Log level: We could add a logLevel option, to allow filtering the log messages while still keeping the default logger.
Test framework: A better test framework (for example Jest) should be used in order to for example have a test coverage report.
Examples: A showcase application could be built as a demo, showing the interest of using a custom logger and broker in a real-case example.
FAQs
A simple event bus library, without dependencies
We found that simple-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.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.