# roof-bus

> roof bus library

Latest version **0.0.18** (published 2016-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install roof-bus
pnpm add roof-bus
yarn add roof-bus
bun add roof-bus
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.18 |
| Published | 2016-01-18 |
| First published | 2015-05-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+7 in 2 direct dependencies) |
| Install scripts | no |
| Author | sskyy |
| Maintainers | sskyy |
| Keywords | event |

## Links

- npm: https://www.npmjs.com/package/roof-bus
- npm.io page: https://npm.io/package/roof-bus

## Dependencies (6)

- [co](https://npm.io/package/co.md) ~4.5.4
- [lodash](https://npm.io/package/lodash.md) ~3.10.1
- [lodash.uniq](https://npm.io/package/lodash.uniq.md) ~3.2.1
- [lodash.merge](https://npm.io/package/lodash.merge.md) ~3.3.1
- [babel-runtime](https://npm.io/package/babel-runtime.md) ~5.8.20
- [lodash.clonedeep](https://npm.io/package/lodash.clonedeep.md) ~3.0.1

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.0.18 (latest) — 2016-01-18
- 0.0.17 — 2016-01-18
- 0.2.0 — 2015-11-24
- 0.1.5 — 2015-11-12
- 0.0.16 — 2015-11-05
- 0.0.15 — 2015-11-02
- 0.0.14 — 2015-11-02
- 0.1.4 — 2015-10-29
- 0.1.3 — 2015-10-14
- 0.1.2 — 2015-10-12
- 0.1.1 — 2015-10-12
- 0.0.13 — 2015-09-20
- 0.0.12 — 2015-09-20
- 0.0.11 — 2015-08-30
- 0.0.10 — 2015-07-07
- … 9 more at https://npm.io/package/roof-bus/versions

## README

# roof-bus

roof-bus is a event bus for both practical programmers and application architects. It can be used as a simple string-based event bus whitout any buy-in in small apps. When handling large apps, especially the one with complex business logic, its powerful features like Listener Sequence Control can help you keep the system low-coupled and flexsible during the growth.

## Quick Start

roof-bus is really easy to get start with, check the code below. I don't think it need any explaination.

```javascript
var Bus = require('roof-bus')
var bus = new Bus

bus.on('start', function busListener( arg1, arg2 ){
	console.log('listener fired', arg1, arg2)
})

bus.fire('start', 'argument1', 'argument2')
```

Most event libraries use a simple first-in, first-out listener sequence. But sometimes simple tactic can not solve real-world problem. For example, framework usually load modules from file system in alphabetical order, but module's listener may require a diffrent registration order. Listener which control the access of certain functionality may require to be called first of the sequence. 

With roof-node, you can do it easily. Say that we have two listeners in different file both listen to event `start`, but one of them is in charge of validating the  arguments.

start-something.js:

```javascript
module.exports = function(bus){
	bus.on('start', function( owner ){
		console.log(owner + 'started something')
	})
}
```

validate-arguments.js:

```javascript
module.exports = function(bus){
	bus.on('start',{
		fn: function(owner){
			if(owner !== 'Jane') throw new Error('owner is not Jane')
		},
		first : true
	})
}
```

To put the validation listener to first place, simply change the listener to a object with attribute `first` that set to true. If you want to stop following listeners from being called, just throw a error. 

Sometime to describe complex business flow, listeners need to fire another event, like:

```javascript
bus.on('process1', function(){
	bus.fire('process2')
})
```

When the hierachy goes deep, it will be hard to figure out what exactly happend when top event fired. Don't worry, roof-bus generates a detailed tracestack every time. And some visualization tools have already been developed.

```
pic
```

Read on for more usage, you may find more practical features.


## Usage

### 1. Simple `on` and `fire`.


```javascript
var Bus = require('roof-bus')
var bus = new Bus

bus.on('start', function busListener( arg1, arg2 ){
	console.log('listener fired', arg1, arg2)
})

bus.fire('start', 'argument1', 'argument2')
```

### 2. Listener Sequence Control

Order of listeners on the same event can be controlled. Just name you listener function and then use the function name in attribute `before` or `after`.

```javascript
bus.on('start', function listener1( arg1, arg2 ){
	console.log('listener1 fired', arg1, arg2)
})

bus.on('start', {
	fn: function listener2( arg1, arg2 ){
		console.log('listener2 fired', arg1, arg2)
	},
	before : ['listener1']
})

bus.fire('start', 'argument1', 'argument2')
```

There are four order control attributes: `before` `after` `first` `last`. Check below.

```javascript
bus.on('start', function listener1( arg1, arg2 ){
	console.log('listener1 fired', arg1, arg2)
})

bus.on('start', {
	fn: function listener2( arg1, arg2 ){
		console.log('listener2 fired', arg1, arg2)
	},
	before : ['listener1']
})

bus.on('start', {
	fn: function listener3( arg1, arg2 ){
		console.log('listener3 fired', arg1, arg2)
	},
	after : ['listener1']
})

bus.on('start', {
	fn: function listener4( arg1, arg2 ){
		console.log('listener4 fired', arg1, arg2)
	},
	first : true
})

bus.fire('start', 'argument1', 'argument2')
//fire order: 4 2 1 3
```

### 3. Handle asynchronicity

`fire` method always return a promise. If you have synchronous code in listener and want roof-bus wait for you, return a promise.

```javascript
bus.on('start', function listener1( arg1, arg2 ){
	return new Promise(function(resolve,reject){
		setTimeout(resolve, 1000)
	})
})


bus.fire('start', 'argument1', 'argument2').then(function(){
	console.log('show in 1 second')
})
```

**Note** that listeners are fired synchronously as default. So listener which returns promise will block followers until promise resolve.

```javascript
bus.on('start', function listener1( arg1, arg2 ){
	return new Promise(function(resolve,reject){
		setTimeout(resolve, 1000)
	})
})

bus.on('start', {
	fn: function listener2( arg1, arg2 ){
		console.log('will wait for listener1 resolve')
	},
	after : ['listener1']
})


bus.fire('start', 'argument1', 'argument2').then(function(){
	console.log('will wait all listener fired')
})

```

If you want some listeners to execute asynchronously, you can set `async` attribute to `true` as below. 


```javascript
bus.on('start', {
	fn: function listener1( arg1, arg2 ){
		return new Promise(function(resolve,reject){
			setTimeout(resolve, 1000)
		})
	},
	async : true
})

bus.on('start', {
	fn: function listener2( arg1, arg2 ){
		console.log('will not be blocked by listener1')
	},
	after : ['listener1']
})



bus.fire('start', 'argument1', 'argument2').then(function(){
	console.log('show in 1 second')
})
```

### 4. Passing data between listeners

Note the order of listeners is important when passing data.

```javascript
bus.on('start', function listener1( arg1, arg2 ){
	this.data.set('name','Bill')
})

bus.on('start', {
	fn: function listener2( arg1, arg2 ){
		console.log(this.data.get('name'))     // 'Bill'
	},
	after : 'listener1'
})
```

### 5. Error handling

You can throw a build-in Error instance or use roof-bus `error` method.

```javascript
bus.on('start', function listener1( arg1, arg2 ){
	throw new Error('some error')
	//or 
	//return this.error(500,{msg:'some error'})
})

bus.fire('start').then(function(){

}).catch(function( error){
	console.log( error)
})

```

## Advanced

### list listeners

### listener tracestack

---
_Source: https://npm.io/package/roof-bus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
