-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventBroker.js
74 lines (65 loc) · 1.82 KB
/
eventBroker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
event broker
clients can register on a channel & provide a object with a callback method
the callbacks get called sequentially whenever we receive an event.
going to store all the callbacks in a hash, so an array of callbacks in a hash of
whenever we get an event on a channel we call all the callbacks associated with it
channels are distinct, there is no hierarchy of channels.
*/
var util = require("util");
CHANNELS = {};
function createEvent(name,channel,payload){
console.log("event created with name = " + name + " and channel = " + channel + " with a payload = " + payload );
var event = {};
event['name'] = name;
event['channel'] = channel;
event['payload'] = payload;
addChannel(channel);
return event;
}
function addChannel(name){
if (CHANNELS[name]) {
console.log("channel already defined");
return false;
} else {
console.log("adding channel " + name);
CHANNELS[name] = [];
return true;
}
}
// the object here has a method called callback which is called
function subscribe(channel,object){
if (CHANNELS[channel]) {
CHANNELS[channel].push(object);
return true;
} else {
return false;
}
}
function unsubscribe(channel,object){
if (CHANNELS[channel]) {
c = CHANNELS[channel].indexOf(object);
if (c >= 0) {
CHANNELS[channel].splice(c,1);
return true;
}
}
console.log("cannot find channel");
return false;
}
function publish(event){
console.log("publishing");
channel = event.channel;
console.log(util.inspect(CHANNELS));
CHANNELS[channel].forEach(
function (object) {
console.log("calling callback");
object.callback(event);
}
);
console.log("finished publishing");
}
exports.createEvent = createEvent;
exports.addChannel = addChannel;
exports.subscribe = subscribe;
exports.publish=publish;