-
Notifications
You must be signed in to change notification settings - Fork 4
/
LocalConnection.js
302 lines (280 loc) · 5.87 KB
/
LocalConnection.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
294
295
296
297
298
299
300
301
302
/**
* LocalConnection
*
* LocalConnection allows callbacks to be triggered across browser tabs and
* windows on the same domain
*/
function LocalConnection(options) {
/**
* Cookie name
*
* @var string
*/
this.name = 'localconnection';
/**
* Unique id for this transmitter
*
* @var integer
*/
this.id = new Date().getTime();
/**
* Whether or not localStorage is supported. Falls back to cookies if not.
*
* @var boolean
*/
this.useLocalStorage = false;
/**
* Log events to the console
*
* @var boolean
*/
this.debug = false;
/**
* List of actions set by addCallback
*
* @var array
*/
this._actions= [];
/**
* Initializes the transmitter
*
* @param opts Object
*/
this.init = function(options) {
// test for localStorage
try {
localStorage.setItem(this.id, this.id);
localStorage.removeItem(this.id);
this.useLocalStorage = true;
} catch(e) {
this.useLocalStorage = false;
}
for (var o in options) {
this[o] = options[o];
}
this.clear();
}
/**
* Starts listening for events
*/
this.listen = function() {
if (this.useLocalStorage) {
if (window.addEventListener) {
window.addEventListener('storage', this.bind(this, this._check), false);
} else {
window.attachEvent('onstorage', this.bind(this, this._check));
}
} else {
setInterval(this.bind(this, this._check), 100);
}
}
/**
* Sends an event with arguments
*
* {{{
* // on receiver
* LocalConnection.addCallback('startVid', myfunction);
* // on sender
* LocalConnection.send('startVid', '#video');
* }}}
*
* @param event string The event name as defined by the receiver
* @param ...rest Other arguments as to be passed to the function
*/
this.send = function(event) {
var args = Array.prototype.slice.call(arguments, 1);
return this._write(event, args);
}
/**
* Adds a callback to a receive event
*
* {{{
* // on receiver
* function myfunction(vidid) {
* $(vidid).play();
* }
* LocalConnection.addCallback('startVid', myfunction);
* // on sender
* LocalConnection.send('startVid', '#video');
* }}}
*
* @param event string The name of the event
* @param func function The callback
*/
this.addCallback = function(event, func, scope) {
if (scope == undefined) {
scope = this;
}
if (this._actions[event] == undefined) {
this._actions[event] = [];
}
this._actions[event].push({f: func, s: scope});
}
/**
* Removes a callback
*
* @param event string The event to stop polling for
*/
this.removeCallback = function(event) {
for (var e in this._actions) {
if (e == event) {
delete this._actions[e];
break;
}
}
}
/**
* Checks for new data
*/
this._check = function() {
var data = this._read();
if (data.length > 0) {
for (var e in data) {
this._receive(data[e].event, data[e].args);
}
}
}
/**
* Called when data is received
*
* @param event string The event name
* @param args array Arguments to pass to the event
*/
this._receive = function(event, args) {
if (this._actions[event] != undefined) {
for (var func in this._actions[event]) {
if (this._actions[event].hasOwnProperty(func)) {
this.log('Triggering callback "'+event+'"', this._actions[event]);
var callback = this._actions[event][func];
callback.f.apply(callback.s, args);
}
}
}
};
/**
* Writes the cookie. Will append if there is already information
*
* @param event string Event name
* @param args array Array of arguments
*/
this._write = function(event, args) {
var events = this._getEvents();
var evt = {
id: this.id,
event: event,
args: args
};
events.push(evt);
this.log('Sending event', evt);
if (this.useLocalStorage) {
localStorage.setItem(this.name, JSON.stringify(events));
} else {
document.cookie = this.name + '=' + JSON.stringify(events) + "; path=/";
}
return true;
}
/**
* Reads the cookie
*
* Returns false if the cookie is empty (i.e., no new data). If new data is found,
* it will return an array of events sent
*/
this._read = function() {
var events = this._getEvents();
if (events == '') {
return false;
}
var ret = [];
// only return events from other connections
for (var e in events) {
if (events[e].id != this.id) {
ret.push({
event: events[e].event,
args: events[e].args
});
events.splice(e, 1);
}
}
if (this.useLocalStorage) {
localStorage.setItem(this.name, JSON.stringify(events));
} else {
document.cookie = this.name + '=' + JSON.stringify(events) + "; path=/";
}
return ret;
}
/**
* Gets all queued events
*
* @return string
*/
this._getEvents = function() {
return this.useLocalStorage ? this._getLocalStorage() : this._getCookie();
}
/**
* Gets raw localStorage data
*
* @return string
*/
this._getLocalStorage = function() {
var events = localStorage.getItem(this.name);
if (events == null) {
return [];
}
return JSON.parse(events);
}
/**
* Gets raw cookie data
*
* @return string
*/
this._getCookie = function() {
var ca = document.cookie.split(';');
var data;
for (var i=0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(this.name+'=') == 0) {
data = c.substring(this.name.length+1, c.length);
break;
}
}
data = data || '[]';
return JSON.parse(data);
}
/**
* Clears all events
*/
this.clear = function() {
if (this.useLocalStorage) {
localStorage.removeItem(this.name);
} else {
document.cookie = this.name + "=; path=/";
}
}
/**
* Binds a function to a scope
*
* @param scope Object The scope
* @param fn Function The function
* @return Function
*/
this.bind = function(scope, fn) {
return function () {
fn.apply(scope, arguments);
};
}
/**
* Logs to the console if it exists
*/
this.log = function() {
if (!this.debug) {
return;
}
if (console) {
console.log(Array.prototype.slice.call(arguments));
}
}
this.init(options);
}