nano-event-bus
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

npm version

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>

Package Sidebar

Install

npm i nano-event-bus

Weekly Downloads

0

Version

1.1.1

License

MIT

Unpacked Size

12.7 kB

Total Files

8

Last publish

Collaborators

  • glebcha