1.0.4 • Published 3 years ago
svea-bus v1.0.4
SveaBus
SveaBus is a lightweight event bus library for handling events.
Installation
You can install SveaBus via npm or yarn:
npm install sveabusor
yarn add sveabusUsage
Import the sveaBus function and create a new event bus instance:
import { sveaBus } from 'sveabus';
const bus = sveaBus();or
import { sveaBus } from 'sveabus';
const {
on,
off,
emit,
clear,
eventHandlers,
} = sveaBus();Event Registration
To register an event handler, use the on method:
bus.on('fire', (data) => {
console.log('Event received:', data);
});Event Emission
To emit an event, use the emit method:
bus.emit('fire', 'whats uo?');Event Handler Removal
To remove an event handler, use the off method:
const handler = (data) => {
// ...
};
bus.on('fire', handler);
// Later, when you want to remove the event handler
bus.off('fire', handler);Clearing Event Handlers
To clear all event handlers, use the clear method:
bus.clear();