Giter Site home page Giter Site logo

nanobus's Introduction

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([name])

Create a new nanobus instance. Optionally takes a name that will be used for tracing in the browser using the performance.mark / performance.measure API.

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]))

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

Listen to an event. If the event name is '*' the listener signature is listener(eventName, [data], [performanceTimingId]).

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

Listen to an event, but make sure it's pushed to the start of the listener queue. If the event name is '*' the listener signature is listener(eventName, [data]).

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

Listen to an event, and clear it after it's been called once. If the event name is '*' the listener signature is listener(eventName, [data], [performanceTimingId]).

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

Listen to an event, and clear it after it's been called once. If the event name is '*' the listener signature is listener(eventName, [data]).

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

nanobus's People

Contributors

yoshuawuyts avatar timmak avatar s3ththompson avatar tornqvist avatar greggraf avatar hughsk avatar jakeburden avatar juliangruber avatar nichoth avatar gesslerpd avatar toddself avatar lordvlad avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.