Changeset View
Changeset View
Standalone View
Standalone View
webroot/rsrc/externals/javelin/lib/Leader.js
Show All 27 Lines | |||||
* leading tab, by listening for `onBecomeLeader`. | * leading tab, by listening for `onBecomeLeader`. | ||||
*/ | */ | ||||
JX.install('Leader', { | JX.install('Leader', { | ||||
events: ['onBecomeLeader', 'onReceiveBroadcast'], | events: ['onBecomeLeader', 'onReceiveBroadcast'], | ||||
statics: { | statics: { | ||||
_leaseDuration: 1500, | |||||
_interval: null, | _interval: null, | ||||
_timeout: null, | _timeout: null, | ||||
_broadcastKey: 'JX.Leader.broadcast', | _broadcastKey: 'JX.Leader.broadcast', | ||||
_leaderKey: 'JX.Leader.id', | _leaderKey: 'JX.Leader.id', | ||||
/** | /** | ||||
* Tracks leadership state. Since leadership election is asynchronous, | * Tracks leadership state. Since leadership election is asynchronous, | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | _callIf: function(leader_callback, follower_callback) { | ||||
// If the lease is good, we're all set. | // If the lease is good, we're all set. | ||||
var now = +new Date(); | var now = +new Date(); | ||||
if (lease.until > now) { | if (lease.until > now) { | ||||
if (lease.id === self._id) { | if (lease.id === self._id) { | ||||
// If we haven't installed an update timer yet, do so now. This will | // If we haven't installed an update timer yet, do so now. This will | ||||
// renew our lease every 5 seconds, making sure we hold it until the | // renew our lease every 5 seconds, making sure we hold it until the | ||||
// tab is closed. | // tab is closed. | ||||
if (!self._interval && lease.until > now + 10000) { | var interval = parseInt(self._leaseDuration / 3, 10); | ||||
self._interval = window.setInterval(self._write, 5000); | |||||
if (!self._interval && lease.until > now + (interval * 2)) { | |||||
self._interval = window.setInterval(self._write, interval); | |||||
} | } | ||||
self._becomeLeader(); | self._becomeLeader(); | ||||
leader_callback(); | leader_callback(); | ||||
} else { | } else { | ||||
// Set a callback to try to become the leader shortly after the | // Set a callback to try to become the leader shortly after the | ||||
// current lease expires. This lets us quickly recover from cases | // current lease expires. This lets us quickly recover from cases | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | statics: { | ||||
/** | /** | ||||
* Write a lease which names us as the leader. | * Write a lease which names us as the leader. | ||||
*/ | */ | ||||
_write: function() { | _write: function() { | ||||
var self = JX.Leader; | var self = JX.Leader; | ||||
var str = [self._id, ((+new Date()) + 16000)].join(':'); | var str = [self._id, ((+new Date()) + self._leaseDuration)].join(':'); | ||||
window.localStorage.setItem(self._leaderKey, str); | window.localStorage.setItem(self._leaderKey, str); | ||||
}, | }, | ||||
/** | /** | ||||
* Read the current lease. | * Read the current lease. | ||||
*/ | */ | ||||
_read: function() { | _read: function() { | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | statics: { | ||||
}, | }, | ||||
/** | /** | ||||
* Try to usurp leadership position after a lease expiration. | * Try to usurp leadership position after a lease expiration. | ||||
*/ | */ | ||||
_usurp: function() { | _usurp: function() { | ||||
var self = JX.Leader; | var self = JX.Leader; | ||||
self.call(JX.bag); | |||||
self._timeout = null; | self._timeout = null; | ||||
self.call(JX.bag); | |||||
}, | }, | ||||
/** | /** | ||||
* Mark a message as seen. | * Mark a message as seen. | ||||
* | * | ||||
* We keep a fixed-sized list of recent messages, and let old ones fall | * We keep a fixed-sized list of recent messages, and let old ones fall | ||||
* off the end after a while. | * off the end after a while. | ||||
Show All 14 Lines |