UNPKG

behave-events

Version:

An extended EventEmitter class with advanced event support (transactions, commands, and requests)

111 lines (75 loc) 2.75 kB
# behave-events An extended EventEmitter class with advanced event support (transactions, commands, and requests) [ ![Codeship Status for behavejs/behave-events](https://codeship.com/projects/bf857f30-8346-0132-a1e3-366b1854f7f3/status?branch=master)](https://codeship.com/projects/58100) `behave-events` is an event module that extends Node's `EventEmitter` class. It adds some common event patterns to the class, making it a fully featured event solution. ### EventEmitter If you are unfamiliar with Node's [EventEmitter](http://nodejs.org/api/events.html) class, I would suggest reading the documentation! ___ ### request / response / stopResponding Easily set up reqres in your application with `response`, `request`, and `stopResponding`. Great for getting access to otherwise inaccessible state. ```javascript let collection = [{a: 1}, {a: 2}, {a: 3}]; class MyClass { constructor() { this.events = new BehaveEvents(); this.events.response('privateCollection', () => { return collection; }); } // ... destroy() { this.events.stopResponding('privateCollection'); } } // ... var myClass = new MyClass(); this.events.request('privateCollection'); // => [{a: 1}, {a: 2}, {a: 3}] // ... myClass.destroy(); this.events.request('privateCollection'); // => undefined ``` ___ ### command / execute / stopExecuting Pass off tasks off to other modules with `command`, `execute`, and `stopExecuting`. Great for things like queues and processing. ```javascript import logger from './logger'; import Router from 'behave-router'; var router = new Router(); router.use((ctx) => { logger.execute('logAsync', { user: ctx.userId, route: ctx.canonicalPath }); }); // ... ``` ___ ### transaction / transact / stopTransacting Sometimes you need to pass off work to another module, but you need confirmation that the task was completed. Some people use `request` for this, but I feel that violates the contract of `reqres`, with `transaction`, `transact`, and `stopTransacting` you can have a declarative way of registering transactions between your modules. ```javascript import billing from './billing'; import logger from './logger'; billing.transact('payment', { userId: 'someid', amount: '100.00', memo: '$$$$' }) .then(receipt => { logger.execute('logAsync', { type: 'payment', user: receipt.user, email: receipt.email, amount: receipt.amount, memo: receipt.memo }); }) .fail(err => { alert('AAAAGGGHHHH! No monies!! $$$$'); }); // ... ``` Release History: - 0.1.0 Initial Release - 0.1.1 Cleaned up examples in readme - 0.1.2 Converted tests to ES6 - 0.1.3 Added build badge