-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e7c801b
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |