JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 52211
  • Score
    100M100P100Q152432F

Tiny message bus

Package Exports

  • nanobus

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nanobus) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nanobus stability

npm version build status test coverage downloads js-standard-style

Tiny message bus.

Usage

var nanobus = require('nanobus')
var bus = nanobus()

bus.on('foo', function (color) {
  console.log('color is', color)
})

bus.emit('foo', 'blue')

FAQ

Why not use the Node API?

We had the requirement for a * event to catch all calls, and figured we could improve the file size at the same time. This library is about 1/3rd the size of Node's version. And it was easy to build, so yeah good enough of an excuse hah.

How do I listen for replies?

You can do this by using the .once() listener and establishing a convention around naming schemas.

bus.on('foo', function (color) {
  console.log('foo called')
  bus.emit('foo:res')
})

bus.once('foo:res', function () {
  console.log('response received')
})
bus.emit('foo')

When shouldn't I use this package?

If you're only writing code that runs inside Node and don't need a '*' listener, consider using the built-in event emitter API instead.

Are the emitters asynchronous?

No. If you're interested in doing that, use something like nanotick to batch events and ensure they run asynchronously.

API

bus = nanobus()

Create a new nanobus instance

bus.emit(eventName, [data])

Emit an event. Arbitrary data can optionally be passed as an argument. '*' listeners run after named listeners.

bus.on(eventName, listener([data]))

Listen to an event.

bus.addListener(eventName, listener(eventName, [data]))

Listen to an event.

bus.on('*', listener(eventName, [data]))

Listen to '*' if you want to subscribe to all events.

bus.addListener('*', listener(eventName, [data]))

Listen to '*' if you want to subscribe to all events.

bus.once(eventName, listener([data]))

Listen to an event, and clear it after it's been called once.

bus.once('*', listener(eventName, [data]))

Listen to '*' if you want to subscribe to all events.

bus.removeListener(eventName, listener)

Remove a specific listener to an event.

listeners = bus.listeners(eventName)

Return all listeners for a given event

bus.removeAllListeners([eventName])

Remove all listeners to an event. If no event name is passed, removes all listeners on the message bus.

License

MIT