-
Notifications
You must be signed in to change notification settings - Fork 4
/
browbeat.js
418 lines (376 loc) · 11.7 KB
/
browbeat.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//
// # Browbeat
//
(function () {
// "Constants" for keys used in `localStorage`.
var HEARTBEAT_KEY = '_browbeat_heartbeat';
var ELECTION_KEY = '_browbeat_election';
var ELECTION_START_KEY = '_browbeat_election_start';
var CURRENT_KEY = '_browbeat_currentMaster';
var MSG_PREFIX = '_browbeat_msg';
var KEY_PREFIX = '_browbeat_';
//
// ## Constructor
//
// Creates a new browbeat instance. Creating more then one Browbeat instance
// per window is not recommended since storage events are not triggered on
// the tab that initiated the change in `localStorage`. Hence the message
// bus is broken.
//
// * **options**, an object of options. Available options are the properties
// assigned to `this` below.
//
var Browbeat = function Browbeat(options) {
if (!this instanceof Browbeat) { return new Browbeat(options); }
options = options || {};
// How long to wait for a heartbeat before initiating a new election.
// The actual heartbeat will be half of this value.
this.heartbeatTTL = 2000;
// For how long will the election be running?
this.electionTime = 2000;
// Set to `true` to recieve debug output.
this.debug = false;
// Maximum number of messages to garbage collect on each run.
this.gcLimit = 100;
for (var i in options) {
if (typeof this[i] !== 'undefined') this[i] = options[i];
}
this.id = Math.random() * 1000;
this.store = window.localStorage || false;
this.isMaster = false;
this.sanityTimer = null;
this.heartbeatTimer = null;
this.gcTimer = null;
this.listeners = {};
this.heartbeatOffset = Math.random() * 10 + 500;
this.init();
};
//
// ## Log
//
// Debug function for console.logging.
//
Browbeat.prototype.log = function () {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift('[Browbeat]');
if (this.debug) console.log.apply(console, args);
};
//
// ## Init
//
// Initializes the current Browbeat instance. Setups up event listeners and
// checks the current state and acts accordingly.
//
Browbeat.prototype.init = function browbeatInit() {
this.log('ID:', this.id);
// No store means no support, make it the master
if (!this.store) {
return this.becomeMaster();
}
// Hook up storage event listener
var self = this;
function handler(event) { self.storageEvent(event); }
if (window.addEventListener) {
window.addEventListener('storage', handler, false);
}
else {
window.attachEventListener('storage', handler);
}
// Check for ongoing election.
var now = (new Date()).getTime();
var lastHearbeat = this.store.getItem(HEARTBEAT_KEY) || 0;
var election = this.store.getItem(ELECTION_KEY);
var started = this.store.getItem(ELECTION_START_KEY);
if (election && (now - started) < this.electionTime) {
this.log('Ongoing election, casting vote');
return this.castVote();
}
// Check for heartbeat, if fresh, become slave.
else if (now - lastHearbeat < this.heartbeatTTL) {
this.log('Found fresh heartbeat');
return this.becomeSlave();
}
// Start election.
else {
return this.startElection();
}
};
//
// ## Handle Storage Event
//
// The storage event is used as a message bus between all open tabs. Thus
// this method acts as kind of a message dispatcher.
//
Browbeat.prototype.storageEvent = function browbeatEventHandler(event) {
var key = event.key;
if (key.indexOf(KEY_PREFIX) !== 0) {
return;
}
// Handle election events.
if (key === ELECTION_KEY) {
// No previous value means a new election was initiated, cast our vote.
if (event.oldValue === null) {
clearTimeout(this.heartbeatTimer);
clearTimeout(this.sanityTimer);
return this.castVote();
}
}
if (key === CURRENT_KEY) {
if (event.newValue === this.id.toString()) {
return this.becomeMaster();
}
else {
return this.becomeSlave();
}
}
// Handle heartbeat events. Check for dead masters.
if (!this.isMaster && key === HEARTBEAT_KEY) {
var self = this;
clearTimeout(this.heartbeatTimer);
this.heartbeatTimer = setTimeout(function () {
self.startElection();
}, this.heartbeatTTL + this.heartbeatOffset);
return;
}
if (key.indexOf(MSG_PREFIX) === 0) {
var data = JSON.parse(event.newValue);
switch (data.message) {
case 'master':
if (this.isMaster) { this.emit('message', data.data); }
break;
case 'slave':
if (!this.isMaster) { this.emit('message', data.data); }
break;
case 'broadcast':
this.emit('message', data.data);
break;
default:
this.emit(data.message, data.data);
break;
}
return;
}
};
// -------------------------------------------------------------------------
//
// ## Become Master
//
// Becomes the master window. Initiate heartbeat and emit event.
//
Browbeat.prototype.becomeMaster = function browbeatElected() {
this.log('Became master');
var self = this;
this.isMaster = true;
this.emit('wonElection');
if (this.store) {
this.heartbeatTimer = setInterval(function heartbeat() {
self.store.setItem(HEARTBEAT_KEY, (new Date()).getTime());
}, this.heartbeatTTL / 2);
// Garbage collect messages older then 2 seconds
this.gcTimer = setInterval(function garbageCollect() {
var now = (new Date()).getTime();
var len = self.store.length;
for (var i = len; i >= 0; i--) {
if (i > self.gcLimit) break;
var key = self.store.key(i);
if (key && key.indexOf(MSG_PREFIX) === 0) {
var parts = key.split('~');
if (now - parseInt(parts[1], 10) > 2000) {
self.store.removeItem(key);
console.log('collected', key);
}
}
}
}, Math.random() * 20000);
}
};
//
// ## Resign
//
// Resigns presidency and let the other windows initiate a new election.
// Assigns a new ID to avoid the same outcome after a vote.
//
Browbeat.prototype.resign = function browbeatResign() {
if (!this.master) return;
this.id = Math.random() * 1000;
this.isMaster = false;
this.emit('resigned');
clearInterval(this.heartbeatTimer);
this.becomeSlave();
};
//
// ## Become Slave
//
// Did not win election. Monitor heartbeat and react to dead master.
//
Browbeat.prototype.becomeSlave = function browbeatBecomeSlave() {
this.log('Became slave');
this.isMaster = false;
this.emit('lostElection');
var self = this;
clearTimeout(this.heartbeatTimer);
this.heartbeatTimer = setTimeout(function () {
self.startElection();
}, this.heartbeatTTL + this.heartbeatOffset);
};
//
// ## Cast Vote
//
// Register as a candidate in the election.
//
Browbeat.prototype.castVote = function browbeatVote() {
clearTimeout(this.sanityTimer);
this.log('Casting vote');
this.emit('voting');
var votes = this.store.getItem(ELECTION_KEY);
votes = votes ? votes.split(',') : [];
votes.push(this.id);
this.store.setItem(ELECTION_KEY, votes);
// Sometimes the initiating window will disappear before the election is
// completed. To avoid a stalemate add a sanity check here.
var self = this;
this.sanity = setTimeout(function () {
if (!self.store.getItem(CURRENT_KEY)) {
self.startElection();
}
}, this.electionTime + this.heartbeatOffset);
};
//
// ## Start Election
//
// Initiates a new election by writing to the localStorage. Since storage
// events are not emitted to the window that initiated the event this method
// also casts a vote.
//
Browbeat.prototype.startElection = function browbeatStartElection() {
this.log('Initiating election');
this.emit('electionInitiated');
var self = this;
this.store.removeItem(CURRENT_KEY);
this.store.removeItem(HEARTBEAT_KEY);
this.castVote();
this.store.setItem(ELECTION_START_KEY, (new Date()).getTime());
setTimeout(function endElection() {
self.emit('electionConcluded');
var candidates = self.store.getItem(ELECTION_KEY);
candidates = candidates ? candidates.split(',') : [self.id];
var winner = Math.max.apply(Math, candidates);
self.store.setItem(CURRENT_KEY, winner);
self.store.removeItem(ELECTION_KEY);
self.store.removeItem(ELECTION_START_KEY);
if (winner === self.id) {
self.becomeMaster();
}
}, this.electionTime);
};
// -------------------------------------------------------------------------
//
// ## On Event
//
// Custom event emitter functionality. Attach a handler to the given event.
//
Browbeat.prototype.on = function browbeatEventOn(e, handler) {
if (!this.listeners[e]) {
this.listeners[e] = [];
}
this.listeners[e].push(handler);
};
//
// ## Emit Event
//
// Emits an event to the registered listeners.
//
Browbeat.prototype.emit = function browbeatEventEmit(e, data) {
if (!this.listeners[e]) return;
data = data || {};
data.eventName = e;
for (var i in this.listeners[e]) {
this.listeners[e][i](data);
}
};
//
// ## Remove Event Listener
//
// Removes the given event listener from the given event. The function
// supplied here must be the exact same function supplied to `on()`.
//
Browbeat.prototype.off = function browbeatEventOff(e, handler) {
if (!this.listeners[e]) {
this.listeners[e] = [];
}
for (var i in this.listeners[e]) {
if (this.listeners[e][i] === handler) {
this.listeners[e].splice(i, 1);
break;
}
}
};
// -------------------------------------------------------------------------
//
// ## Broadcast
//
// Broadcast a message to _all_ windows, including the sender.
//
Browbeat.prototype.broadcast = function browbeatBroadcast(message) {
this.emit('message', message);
this.sendMessage('broadcast', message);
};
//
// ## Message Master
//
// Sends a message to the master only.
//
Browbeat.prototype.messageMaster = function browbeatMsgMaster(message) {
if (this.isMaster) {
this.emit('message', message);
}
else {
this.sendMessage('master', message);
}
};
//
// ## Message Slaves
//
// Sends a message to the slaves only.
//
Browbeat.prototype.messageSlaves = function browbeatMsSlaves(message) {
if (!this.isMaster) this.emit('message', message);
this.sendMessage('slave', message);
};
//
// ## Send Message
//
// Sends a message on the "bus" to other tabs. The message is written to the
// `localStorage`. The message will be garbage collected by the master at
// some point.
//
Browbeat.prototype.sendMessage = function browbeatSend(message, data) {
var msg = {
message: message,
data: data,
timestamp: (new Date()).getTime()
};
var key = MSG_PREFIX + '~' + msg.timestamp + '~' + Math.random();
this.store.setItem(key, JSON.stringify(msg));
this.emit('sentMessage', [message, data]);
};
// -------------------------------------------------------------------------
//
// ## Export Module
//
// Try to be a good citizen in whatever environment we find ourselves.
//
(function () {
var hasDefine = typeof define === 'function' && define.amd;
var hasExport = typeof exports !== 'undefined';
if (hasDefine) {
define('Browbeat', Browbeat);
}
else if (hasExport) {
exports = Browbeat;
}
else {
window.Browbeat = Browbeat;
}
}());
}());