-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathporter.js
293 lines (231 loc) · 7.17 KB
/
porter.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict'
function fnToString(fn) {
return Function.prototype.toString.call(fn)
}
var objStringValue = fnToString(Object)
/**
* @param {any} obj The object to inspect.
* @returns {boolean} True if the argument appears to be a plain object.
*/
function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
function isPlainObject(obj) {
if (!obj || typeof obj !== 'object') {
return false
}
var proto = typeof obj.constructor === 'function' ? Object.getPrototypeOf(obj) : Object.prototype
if (proto === null) {
return true
}
var constructor = proto.constructor
return typeof constructor === 'function'
&& constructor instanceof constructor
&& fnToString(constructor) === objStringValue
}
function copy(thing){
if (isPlainObject(thing)) {
var newObj = {};
for (var key in thing) {
newObj[key] = copy(thing[key])
}
return newObj;
}
if (isArray(thing)) {
return thing.map(function(item){ return copy(item);})
}
return thing;
}
var PorterEvent = function(event, params){
var e;
params = params || {
bubbles: false,
cancelable: false,
detail: undefined
};
e = document.createEvent("CustomEvent");
e.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return e;
};
PorterEvent.prototype = window.Event.prototype;
PorterEvent.prototype.copy = function() {
return copy(this.detail);
};
var Channel = function(name) {
/*
The Channel class manages the addition and
removal of EventListeners with actions and callbacks
on the window object.
The Channel class's `publish` method turns actions
and data into dispatched events namespaced to the channel's
name ('theChannelName::theAction')
*/
if (!name instanceof String) {
console.error("Channels only accept strings as names; not", name);
}
this.name = name;
this.subscribers = {};
};
Channel.prototype.formatAction = function(action) {
return [this.name, action].join("::");
};
Channel.prototype.findSubscriber = function(action) {
return this.subscribers[action];
};
Channel.prototype.subscribe = function(action, callback) {
var subscriber = this.findSubscriber(action);
if (subscriber) {
subscriber.push(callback);
} else {
subscriber = [callback];
}
this.subscribers[action] = subscriber;
var chanAction = this.formatAction(action)
window.addEventListener(chanAction, callback);
};
Channel.prototype.unsubscribe = function(action, callback) {
var subscriber = this.findSubscriber(action);
if (subscriber) {
subscriber = subscriber.filter(function(item) { return item != callback; });
this.subscribers[action] = subscriber;
var chanAction = this.formatAction(action)
window.removeEventListener(chanAction, callback);
}
};
Channel.prototype.publish = function(action, data) {
var chanAction = this.formatAction(action);
var event = new PorterEvent(chanAction, {"detail": data});
window.dispatchEvent(event);
};
Channel.prototype.clearSubscribers = function() {
var self = this;
for(var key in this.subscribers) {
if(Object.prototype[key]) return;
this.subscribers[key].forEach(function(callback) {
self.unsubscribe(key, callback)
});
}
this.subscribers = {};
};
// var Porter = function (name) {
// this.channel = new Channel(name || "porter");
// this.channels = {};
// this.connectors = {};
// };
// Porter.prototype.addChannel = function(channel){
// if (channel instanceof Channel) {
// if (channel.name == this.channel.name) {
// console.error("Porter: the channel", channel.name, "already belongs to this instance of Porter");
// return;
// }
// this.channels[channel.name] = channel;
// } else {
// console.error("Porter: invalid channel - cannot addChannel", channel);
// }
// };
// Porter.prototype.removeChannel = function(channel) {
// if (channel instanceof Channel) {
// delete this.channels[channel.name];
// } else if (channel instanceof String) {
// delete this.channels[channel];
// }
// };
function grab(e) {
return e.detail;
}
var GenericConnector = function(channel){
this.name = "genconn";
this.channels = {};
if (channel) this.addChannel(channel);
};
GenericConnector.prototype.addChannel = function(channel) {
if (channel instanceof Channel) {
this.channels[channel.name] = channel;
} else {
console.error("GenericConnector: invalid channel - cannot addChannel", channel);
}
};
GenericConnector.prototype.removeChannel = function(channel) {
if (channel instanceof Channel) {
delete this.channels[channel.name];
} else if (channel instanceof String) {
delete this.channels[channel];
}
};
GenericConnector.prototype.connect = function() {
var self = this;
//spoofs IO connection
setTimeout(function() {
self.publish("connect", {name: self.name});
}, 10);
};
GenericConnector.prototype.disconnect = function() {
var self = this;
//spoofs IO connection
setTimeout(function() {
self.publish("disconnect", {name: self.name});
}, 10);
};
GenericConnector.prototype.send = function(payload) {
var self = this;
self.publish("send", {name: self.name, payload: payload});
setTimeout(function() {
self.publish("receive", {name: self.name, payload: payload});
}, 1000);
};
GenericConnector.prototype.publish = function(action, payload) {
for(var name in Object.keys(this.channels)) {
this.channels[name].publish(action, payload);
}
}
// Porter.prototype.connect = function (){
// var self = this;
// this.registry.subscribe("incoming_message", function(e) {
// self.execute(e.detail);
// });
// this.connection.connect(this.registry);
// };
// Porter.prototype.disconnect = function() {
// this.connection.disconnect()
// this.registry.clearSubscribers();
// };
// Porter.prototype.subscribe = function(name, listener) {
// this.registry.subscribe(name, listener);
// };
// Porter.prototype.unsubscribe = function(name, listener) {
// this.registry.unsubscribe(name, listener);
// };
// * Broadast details
// * topic type string
// * clientAction type string
// * args array of items
// * Signature:
// * serverAction(serverTarget), clientAction(clientTarget), args
// Porter.prototype.broadcast = function(topic, action, args) {
// var generalbroadcast = "porter::broadcast";
// var topicBroadcast = broadcast + "::" + topic;
// var payload = {"topic": topic, "action": action, "args": args};
// this.registry.publish(broadcast, payload)
// this.registry.publish(topicBroadcast, payload)
// this.connection.send(JSON.stringify(payload));
// };
// /*
// dispatch expects JSON in the form:
// * '{ action: "SomeString", args:["a", "list","of","args",1] }'
// * action type string
// * args type all
// * Signature:
// * function(action, args)
// */
// Porter.prototype.dispatch = function(data) {
// var incomingData = JSON.parse(data);
// this.dispatcher.execute(incomingData.action, incomingData.args);
// };
// Porter: Porter,
// Producer: Producer,
if (typeof module != 'undefined') {
module.exports = {
Channel: Channel,
GenericConnector: GenericConnector
};
}