nano-event-bus
v1.1.1
Published
tiny event bus based on custom events
Downloads
9
Maintainers
Readme
nano-event-bus
Tiny event bus based on custom events (for now only in browser)
on(type: EventType, listener: EventListener, options?: SubscribeOptions): void;
Add subscription with handler to process events by type
once(type: EventType, listener: EventListener): void;
Add subscription with handler to process event by type only once
off(type: EventType, predicate: EventListener | string): void;
Remove subscription for defined event type by predicate (hash id or named function)
allOff(type: EventType): void;
Remove all subscriptions for defined event type
emit(type: EventType, detail?: EventDetail): void;
Emit event with defined type and pass optional data (any object)
Sample usage:
import { EventBus } from 'nano-bus';
const bus = new EventBus();
function addCallback(event) {
console.log(event);
}
bus.on('add', addCallback);
bus.emit('add');
Use with JSDelivr CDN:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/nano-event-bus/build/umd/nano-event-bus.umd.js"></script>
<body>
<script type="text/javascript">console.log(window.NanoEventBus.EventBus);</script>
</body>
Use ES-module with Skypack CDN:
<script type="module">
import { EventBus } from 'https://cdn.skypack.dev/nano-event-bus';
Object.defineProperty(window, 'EventBus', {
value: Object.freeze(new EventBus()),
writable: false,
configurable: false
});
console.log(window.EventBus)
</script>