1.0.0 • Published 9 years ago

delegate2 v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Delegate2

Simple proxy-pattern implementation. In a dynamic environment like node you don't want to inherit from classes, since changes in the parent will likely break your code. Instead you want to use delegation. This module makes it really simple.

Example usage

var EventEmitter = require("events").EventEmitter,
    delegate = require("delegate2");

function NumGenerator() {
    var self = this;

    this.EM = new EventEmitter();
    delegate(this, this.EM, ["on", "emit"]);

    setInterval(function() {
        self.emit("nextNum", Math.random());
    }, 300);
}

var gen = new NumGenerator();

gen.on("nextNum", function(num) {
    console.log(num);
});

FAQ

How do I delegate all the methods that the receiver implements?

You don't.

Why ?!

You can't know if the admin of the EventEmitter doesn't add a new method that has the same name as one of yours and you end up with your method overriden.

But then why do we use versioning?

Javascript devs don't follow semver even if they say so.