• Stars
    star
    225
  • Rank 177,187 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🚎 - Tiny message bus

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. '*' listeners are not included in this list. Use bus.listeners('*') to get a list of '*' listeners.

bus.removeAllListeners([eventName])

Remove all listeners to an event. If no event name is passed, removes all listeners on the message bus. '*' listeners are not removed unless eventName is * or no event name is passed.

TypeScript

Optional event typing is available in TypeScript by passing an object type with event names as keys and event listener function signatures as values.

// if compilerOptions.esModuleInterop = true
import Nanobus from "nanobus"
// else
import Nanobus = require("nanobus") 

type Events = {
    foo: (color: string) => void
    bar: (count: number) => void
}

const bus = new Nanobus<Events>()

bus.on("foo", color => {
    // color: string
    console.log("color is", color)
})

bus.on("bar", count => {
    // count: number
    console.log("count is", count)
})

bus.on("*", (eventName, data) => {
    // eventName: "foo" | "bar"
    // data: any[]
    if (eventName === "foo") {
        const [color] = data as Parameters<Events["foo"]>
        // color: string
    } else if (eventName === "bar") {
        const [count] = data as Parameters<Events["bar"]>
        // count: number
    }
})

bus.emit("foo", "blue")  // required arguments: [string]
bus.emit("bar", 100)  // required arguments: [number]

License

MIT

More Repositories

1

choo

🚂🚋 - sturdy 4kb frontend framework
JavaScript
6,776
star
2

bankai

🚉 - friendly web compiler
JavaScript
1,088
star
3

hyperx

🏷 - tagged template string virtual dom builder
JavaScript
1,010
star
4

nanomorph

🚅 - Hyper fast diffing algorithm for real DOM nodes
JavaScript
726
star
5

nanohtml

🐉 HTML template strings for the Browser with support for Server Side Rendering in Node.
JavaScript
687
star
6

nanographql

Tiny graphQL client library
JavaScript
421
star
7

nanocomponent

🚃 - create performant HTML components
JavaScript
366
star
8

wayfarer

👓 composable trie based router
JavaScript
332
star
9

choo-handbook

🚂✋📖 - Learn the choo framework through a set of exercises
HTML
268
star
10

awesome-choo

🌅 Awesome things related with choo framework
197
star
11

create-choo-app

🚞 - create a fresh choo application
JavaScript
181
star
12

nanostate

🚦- Small Finite State Machines
JavaScript
170
star
13

nanorouter

🛤 - Small frontend router
JavaScript
116
star
14

nanocomponent-adapters

🔌 - Convert a nanocomponent to a component for your favourite API or library (web components, (p)react, angular)
JavaScript
96
star
15

choop

🚂⚛️ - choo architecture for preact
JavaScript
93
star
16

on-idle

😴 - Detect when the browser is idle
JavaScript
82
star
17

nanologger

📜 - Cute browser logs
JavaScript
80
star
18

nanoanimation

👨‍🎨 - Safety wrapper around the Web Animation API
JavaScript
72
star
19

nanoraf

🎞 - Only call RAF when needed
JavaScript
71
star
20

choo-devtools

💼 - Expose a choo instance on the window
JavaScript
53
star
21

nanoquery

📇 - Tiny querystring module
JavaScript
49
star
22

nanotask

Microtask queue scheduler for the browser
JavaScript
47
star
23

choo-log

📃 - Development logger for choo
JavaScript
47
star
24

nanoscheduler

Schedule work to be completed when the user agent is idle.
JavaScript
46
star
25

website

🚇 - Hyper Train Transfer Protocol (HTTP)
JavaScript
46
star
26

nanohref

⛓ - Tiny href click handler library
JavaScript
41
star
27

nanotick

process.nextTick() batching utility
JavaScript
37
star
28

choo-store

Lightweight state structure for choo apps.
JavaScript
37
star
29

nanotiming

⏲ - Small timing library
JavaScript
35
star
30

create-choo-electron

:electron: - Create a fresh Choo Electron application
JavaScript
29
star
31

object-change-callsite

Determine the callsite of an object change using Proxies
JavaScript
27
star
32

choo-reload

⛽️ - Livereloading package for choo
JavaScript
27
star
33

on-performance

Listen for performance timeline events
JavaScript
26
star
34

nanobeacon

Small navigator.sendBeacon wrapper
JavaScript
25
star
35

choo-service-worker

👷 - Service worker loader for choo
JavaScript
24
star
36

choo-scaffold

🏗 - Scaffold out files for a Choo project
JavaScript
24
star
37

choo-notification

Web Notification plugin for Choo
JavaScript
22
star
38

nanobounce

Smol debounce package
JavaScript
19
star
39

choo-choo

🎓 learn choo from the command line!
JavaScript
19
star
40

nanomount

Mount a DOM tree on a target node
JavaScript
19
star
41

choo-redirect

🎬 - Redirect a view to another view
JavaScript
19
star
42

persist-storage

🗄 - Enable persistent storage in the browser
JavaScript
19
star
43

nanohistory

Small browser history library
JavaScript
14
star
44

choo-hooks

🎣 - Hook into Choo's events and timings
JavaScript
12
star
45

nanolocation

📍- Small window.location library
JavaScript
10
star
46

discuss

🎭 – Discuss project organization, initiatives, and anything else!
8
star
47

nanocache

Cache Nanocomponents.
JavaScript
7
star
48

bankai-website

JavaScript
6
star
49

choo-umd

🙈 - umd build for choo framework
HTML
3
star