Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
azer committed Feb 11, 2011
0 parents commit e7c801b
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
observer.js
===========
An implementation of the observer design pattern.

Tested Platforms: V8 (Node)

From NPM
========
npm install observer

Usage
=====
To create a pub/sub table:
var EventBroker = require('observer').EventBroker;

var events = new EventBroker;
events.create('foobar');

events.subscribe('foobar', console.log.bind(console,'observer#1:') );
events.subscribe('foobar', console.log.bind(console,'observer#2:') );

events.publish('foobar',3,14);

// => observer#1, 3, 14
// => observer#2, 3, 14

To create an observable class:
var Observable = require('observer').Observable,
inherits = require('util').inherits;

function Foobar(){
Observable.call(this);
this.create('qux');
}

var f = new Foobar();
f.on('qux', console.log.bind(console, 'observer#3:');
f.events.publish('qux',3,14);

// => observer#3, 3, 14

Testing
=======
node test/test.js
44 changes: 44 additions & 0 deletions lib/observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var EventBroker = exports.EventBroker = function EventBroker(){
this.subjects = {};
};

EventBroker.prototype.subscribe = function(subject,callback){
this.get(subject).push(callback);
};

EventBroker.prototype.create = function(subject){
this.subjects[subject] = [];
};

EventBroker.prototype.get = function(subject){
if( !this.has(subject) ) {
throw new Error("Subject Not Found: "+subject);
}

return this.subjects[subject];
};

EventBroker.prototype.has = function(subject){
return this.subjects.hasOwnProperty(subject);
}

EventBroker.prototype.publish = function(subject){
var subscribers = this.get(subject),
args = Array.prototype.slice.call(arguments,1);

args.splice(0,0, undefined);

for(var i = -1, len=subscribers.length; ++i < len; ){
setTimeout(Function.prototype.bind.apply(subscribers[i], args), 0);
};
};

function Observable(){
this.events = new EventBroker;
this.on = this.events.subscribe.bind(this.events);
};

module.exports = {
'Observable':Observable,
'EventBroker':EventBroker
};
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name":"observer",
"version":"1.0.0",
"description":"An implementation of observer design pattern.",
"author":"Azer Koculu <azer@kodfabrik.com>",
"keywords":["observer","pubsub","events"],
"directories":{ "lib": "./lib" },
"main":"./lib/observer"
}
42 changes: 42 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var assert = require('assert'),
inherits = require('util').inherits,
Observable = require('../lib/observer').Observable;

function Cal(){
Observable.call(this);

this.events.create('sum');
this.events.create('sub');
this.events.create('mul');
}

inherits(Cal, Observable);

Cal.prototype.sum = function(x,y){
this.events.publish('sum', x+y);
};

Cal.prototype.sub = function(x,y){
this.events.publish('sub',y-x);
};

Cal.prototype.mul = function(x,y){
this.events.publish('mul', x*y);
async = true;
};

var pi = new Cal(),
async = false;

pi.on('sum', pi.sub.bind(pi,0.43));
pi.on('sub', pi.mul.bind(pi,2));
pi.on('mul', function(val){
assert.ok(async);
pi.value = val;
});

pi.sum(1.3,0.7);

process.addListener('exit', function(){
assert.ok(pi.value, 3.14);
});

0 comments on commit e7c801b

Please sign in to comment.