Tiny event bus based on custom events (for now only in browser)
Add subscription with handler to process events by type
Add subscription with handler to process event by type only once
Remove subscription for defined event type by predicate (hash id or named function)
Remove all subscriptions for defined event type
Emit event with defined type and pass optional data (any object)
import { EventBus } from 'nano-bus';
const bus = new EventBus();
function addCallback(event) {
console.log(event);
}
bus.on('add', addCallback);
bus.emit('add');
<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>
<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>