-
Notifications
You must be signed in to change notification settings - Fork 7
/
connector.js
108 lines (102 loc) · 2.39 KB
/
connector.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* A connector to other pubsub hubs
**/
var hub = require("./hub");
var callbacks = [];
var connections = [];
exports.on = exports.observe = function(name, callback){
connections.forEach(callback);
callbacks.push(callback);
}
exports.Connector = function(clientId, connection){
connections.push(connection);
var ourHub = hub.fromClient(clientId);
ourHub.subscribe("**", "monitored", onSubscription);
var subscribers = ourHub.getSubscribers();
for(var channel in subscribers){
var subs = subscribers[channel];
for(event in subs){
var unnamedSent = false;
if(isNaN(event)){
onSubscription({
monitored: true,
channel: channel,
fortype: event
});
}else if(!unnamedSent){
unnamedSent = true;
onSubscription({
monitored: true,
channel: channel
});
}
}
}
connection.on("message", function(message){
if(typeof message.channel === "string"){
if(message.method === "subscribe"){
ourHub.subscribe(message.channel, message.subscribe, function(message){
connection.send(message);
});
}else{
ourHub.publish(message);
}
}
});
callbacks.forEach(function(callback){
callback(connection);
});
function onSubscription(message){
if(message.monitored){
connection.send({
method: "subscribe",
channel: message.channel,
subscribe: message.fortype || "*"
});
}else{
connection.send({
method: "unsubscribe",
channel: channel,
subscribe: event || "*"
});
}
}
};
exports.WorkerConnector = function(workerName){
worker = new (require("worker").SharedWorker)("test.js",workerName);
worker.onmessage = function(event){
callbacks.forEach(function(callback){
callback(event.data);
});
};
var callbacks = [];
var connection = {
send: function(data){
worker.postMessage(data);
},
observe: function(name, callback){
callbacks.push(callback);
}
};
exports.Connector("local-workers", connection);
}
exports.HttpConnector = function(url){
}
/*onmessage = function(event){
var request = JSON.parse(event.data);
var source = event.ports[0];
var sourceName = source.name;
var topic = request.pathInfo;
switch(request.method.toLowerCase()){
case "post":
publish(sourceName, topic, request.body);
break;
case "subscribe":
subscribe(sourceName, topic, function(){
source.postMessage();
});
break;
case "unsubscribe":
unsubscribe(sourceName, topic);
}
}*/