SveaBus is a lightweight event bus library for handling events.
You can install SveaBus via npm or yarn:
npm install sveabus
or
yarn add sveabus
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();
To register an event handler, use the on method:
bus.on('fire', (data) => {
console.log('Event received:', data);
});
To emit an event, use the emit method:
bus.emit('fire', 'whats uo?');
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);
To clear all event handlers, use the clear method:
bus.clear();