node-binder

Mixin class for managing bindings in Node.js

npm install node-binder
10 downloads in the last week
20 downloads in the last month

node-binder

Mixin class for managing bindings in Node.js.

Install

npm install node-binder

Interfaces

interface Binder {
  attribute Array bindings;
  Binding bind(EventEmitter emitter, String event, Function handler, optional any context = emitter);
  Binding once(EventEmitter emitter, String event, Function handler, optional any context = emitter);
  Binding bindKey(EventEmitter emitter, String event, Function handler, String key, optional any context = emitter);
  void destroy()
}
interface Binding {
  attribute Binder binder;
  attribute String event;
  attribute Function handler;
  attribute EventEmitter emitter;
  void unbind()
}

API

Binder

The Binder class helps you manage bindings when gluing various classes and events together. When your program or instance no longer needs these bindings, simply call binder.destroy() to automatically remove all bindings to their respective EventEmitters.

Binder#bind(emitter, event, handler [, context])

Binds handler to the event of emitter.

Aliases: on, addListener

Arguments

  • emitter - Any object that inheriets from EventEmitter.
  • event - Name of the event on emitter to which the handler will be bound.
  • handler - Function handler for the event.
  • context - Optional. Context with which to execute the handler.

Returns Binding

Example

var binding = binder.bind(someEventEmitter, "data", function( chunk ) {
  console.log("got a chunk!");
  console.log(chunk); // "foo"
}, someEventEmitter);

someEventEmitter.emit("data", "foo")

binding.unbind();

Binder#once(emitter, event, handler [, context])

Binds handler to one emission of event on emitter.

Aliases: one

Arguments

  • emitter - Any object that inheriets from EventEmitter.
  • event - Name of the event on emitter to which the handler will be bound.
  • handler - Function handler for the event.
  • context - Optional. Context with which to execute the handler.

Returns Binding

Example

var binding = binder.once(someEventEmitter, "open", function( chunk ) {
  // Open now, do your stuff!
}, someEventEmitter);

someEventEmitter.emit("open")

Binder#bindKey(emitter, event, object, key, [, context])

Binds object[key] to the event of emitter.

Arguments

  • emitter - Any object that inheriets from EventEmitter.
  • event - Name of the event on emitter to which the handler will be bound.
  • handler - Function handler for the event.
  • context - Optional. Context with which to execute the handler.

Returns Binding

Example

var someLiteral = {

  foo: function(data) {
    console.log("beep");
  },

  bar: function() {
    console.log("boop");
  }

};

var binding = binder.bindKey(someEventEmitter, "open", someLiteral, "dataHandler");

someLiteral.dataHandler = someLiteral.foo;
someEventEmitter.emit("data"); // "beep"

someLiteral.dataHandler = someLiteral.bar;
someEventEmitter.emit("data"); // "boop"

Binder#destroy()

Unbinds all events previously bound using bind.

Aliases: removeAllListeners

Example

binder.bind(someStream, "open", someClassThatHandlesStreams.onOpen, someClassThatHandlesStreams);
binder.bind(someStream, "data", someClassThatHandlesStreams.onData, someClassThatHandlesStreams);
binder.bind(someStream, "end", someClassThatHandlesStreams.onEnd, someClassThatHandlesStreams);
binder.bind(someStream, "error", someClassThatHandlesStreams.onError, someClassThatHandlesStreams);

/* Later on when you no longer need someClassThatHandlesStreams… */
binder.destroy();

Binding

All Binder methods that perform a binding return a Binding instance which represents the binding created from the Binder. This Binding instance has only one method, unbind, which removes it's handler from it's EventEmitter.

Binding#unbind()

Unbinds the binding it represents.

Aliases: off, removeListener

Example

var binding = binder.bind(someStream, "open", someClassThatHandlesStreams.onOpen, someClassThatHandlesStreams);

binding.unbind();

License

MIT license

npm loves you