Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

behave-events

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

behave-events

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

  • 0.1.3
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

behave-events

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

Codeship Status for behavejs/behave-events

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 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.

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.


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.


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

Keywords

FAQs

Package last updated on 26 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc