Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (43 sloc) 1.11 KB

mediator

Mediator pattern implementation for Node.js.

example

var Mediator = require('../index'),
    mediator =  new Mediator(),
    // Add a couple of members
    member0 = {},
    member1 = {};

//add the members to the mediator
mediator.add(member0,"testEvent0",member0Callback);
mediator.add(member1,"testEvent0",member1Callback);

//inline callback
mediator.add(member1,"testEvent1",function(rs){
    console.log("-> testEvent1 " + rs.res);
});

//anon member with inline callback
mediator.on("testEvent1", function(rs){
    console.log("-> Anon testEvent1 " + rs.res);
});

//add a few callbacks
function member0Callback (rs){
    console.log("-> member0Callback " + rs.res);
};

function member1Callback (rs){
    console.log("-> member1Callback " + rs.res);
};

//emit some events
member0.emit("testEvent0", {res:"word"});
member1.emit("testEvent1", {res:"yo"});

//removelistener
mediator.remove("testEvent0",member1);
//...and check if is still listening
member0.emit("testEvent0", {res:"word"});

install

With npm do:

npm install mediator