Giter VIP home page Giter VIP logo

event-wire's Introduction

event-wire

Build Status NPM version Coverage Status

Mediator with dynamic responsibility chains.

Idea if this package is to have hybrid of EventEmitter and chain-of-Responsibility. In short - dynamic channels with wildcards, and guaranteed execution order for listeners.

Features:

  • sync & async listeners
  • wildards
  • exclusions

Install

npm install event-wire --save

API

constructor

var wire = new require('event-wire');
// or
var wire = require('event-wire')();

Create new event-wire instanse.

.emit(channels [, obj, callback])

Sends message with obj param into the channels (String|Array). Once all sync and ascync handlers finished, optional callback(err) (if specified) fired.

.on(channels [, options], handler)

Registers handler to be executed upon messages in the a single channel or a sequence of channels stored in channels parameter. Handler can be either sync function:

wire.on('foobar', function () {
  // do stuff here
});

wire.on('foobar', function (obj) {
  // do stuff here
});

Or it might be an async function with callback(err) second argument:

wire.on('foobar', function (obj, callback) {
  // do stuff here
  callback(null);
});

Each handler can termitate chain execution by returning not falsy result (error)

Options:

  • priority (Number, Default: 0) - execution order (lower is earlier). Handlers with equal priorities are executed in definition order.
  • ensure (Boolean, Default: false) - If true, will run handler even if one of previous fired error.
  • name (String) - handler name, if function is anonimous or you need to guarantee it intat after code uglifiers.

.once(channels [, options], handler)

The same as .on(), but executed only one time.

.before(...), .after(...)

Aliases of .on(...), but with priority -10 and +10

.off(channel[, handler])

Removes handler of a channel, or removes ALL handlers of a channel if handler is not given.

.skip(channel, skipList)

Exclude calling list of named handlers for given chennel (wildards allowed):

wire.skip('server:static.*', [
  session_start,
  cookies_start
]);

.has(channel) -> Boolean

Returns if channel (String) has at least one subscriber with zero priority (main handler). Useful for dynamic routing

.stat() -> Array

Returns array of info about every channel. For debug purposes. For example, you can write dumper to check that all expected channels have required handlers. Or to track number of calls.

License

MIT

event-wire's People

Contributors

rlidwka avatar

Stargazers

 avatar

Watchers

 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.