-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
3,138 additions
and
2,391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.beLeader = beLeader; | ||
exports.sendLeaderMessage = sendLeaderMessage; | ||
var _unload = require("unload"); | ||
/** | ||
* sends and internal message over the broadcast-channel | ||
*/ | ||
function sendLeaderMessage(leaderElector, action) { | ||
var msgJson = { | ||
context: 'leader', | ||
action: action, | ||
token: leaderElector.token | ||
}; | ||
return leaderElector.broadcastChannel.postInternal(msgJson); | ||
} | ||
function beLeader(leaderElector) { | ||
leaderElector.isLeader = true; | ||
leaderElector._hasLeader = true; | ||
var unloadFn = (0, _unload.add)(function () { | ||
return leaderElector.die(); | ||
}); | ||
leaderElector._unl.push(unloadFn); | ||
var isLeaderListener = function isLeaderListener(msg) { | ||
if (msg.context === 'leader' && msg.action === 'apply') { | ||
sendLeaderMessage(leaderElector, 'tell'); | ||
} | ||
if (msg.context === 'leader' && msg.action === 'tell' && !leaderElector._dpLC) { | ||
/** | ||
* another instance is also leader! | ||
* This can happen on rare events | ||
* like when the CPU is at 100% for long time | ||
* or the tabs are open very long and the browser throttles them. | ||
* @link https://github.com/pubkey/broadcast-channel/issues/414 | ||
* @link https://github.com/pubkey/broadcast-channel/issues/385 | ||
*/ | ||
leaderElector._dpLC = true; | ||
leaderElector._dpL(); // message the lib user so the app can handle the problem | ||
sendLeaderMessage(leaderElector, 'tell'); // ensure other leader also knows the problem | ||
} | ||
}; | ||
|
||
leaderElector.broadcastChannel.addEventListener('internal', isLeaderListener); | ||
leaderElector._lstns.push(isLeaderListener); | ||
return sendLeaderMessage(leaderElector, 'tell'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.LeaderElectionWebLock = void 0; | ||
var _util = require("./util.js"); | ||
var _leaderElectionUtil = require("./leader-election-util.js"); | ||
/** | ||
* A faster version of the leader elector that uses the WebLock API | ||
* @link https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API | ||
*/ | ||
var LeaderElectionWebLock = function LeaderElectionWebLock(broadcastChannel, options) { | ||
var _this = this; | ||
this.broadcastChannel = broadcastChannel; | ||
broadcastChannel._befC.push(function () { | ||
return _this.die(); | ||
}); | ||
this._options = options; | ||
this.isLeader = false; | ||
this.isDead = false; | ||
this.token = (0, _util.randomToken)(); | ||
this._lstns = []; | ||
this._unl = []; | ||
this._dpL = function () {}; // onduplicate listener | ||
this._dpLC = false; // true when onduplicate called | ||
|
||
this._wKMC = {}; // stuff for cleanup | ||
}; | ||
exports.LeaderElectionWebLock = LeaderElectionWebLock; | ||
LeaderElectionWebLock.prototype = { | ||
hasLeader: function hasLeader() { | ||
return navigator.locks.query().then(function (locks) { | ||
if (locks.held && locks.held.length > 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}); | ||
}, | ||
awaitLeadership: function awaitLeadership() { | ||
var _this2 = this; | ||
if (!this._wLMP) { | ||
this._wKMC.c = new AbortController(); | ||
var returnPromise = new Promise(function (res, rej) { | ||
_this2._wKMC.res = res; | ||
_this2._wKMC.rej = rej; | ||
}); | ||
this._wLMP = new Promise(function (res) { | ||
var lockId = 'pubkey-bc||' + _this2.broadcastChannel.method.type + '||' + _this2.broadcastChannel.name; | ||
navigator.locks.request(lockId, { | ||
signal: _this2._wKMC.c.signal | ||
}, function () { | ||
(0, _leaderElectionUtil.beLeader)(_this2); | ||
res(); | ||
return returnPromise; | ||
}); | ||
}); | ||
} | ||
return this._wLMP; | ||
}, | ||
set onduplicate(_fn) { | ||
// Do nothing because there are no duplicates in the WebLock version | ||
}, | ||
die: function die() { | ||
var _this3 = this; | ||
var ret = (0, _leaderElectionUtil.sendLeaderMessage)(this, 'death'); | ||
this._lstns.forEach(function (listener) { | ||
return _this3.broadcastChannel.removeEventListener('internal', listener); | ||
}); | ||
this._lstns = []; | ||
this._unl.forEach(function (uFn) { | ||
return uFn.remove(); | ||
}); | ||
this._unl = []; | ||
if (this.isLeader) { | ||
this.isLeader = false; | ||
} | ||
this.isDead = true; | ||
if (this._wKMC.res) { | ||
this._wKMC.res(); | ||
} | ||
if (this._wKMC.c) { | ||
this._wKMC.c.abort(); | ||
} | ||
return ret; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { BroadcastChannel, clearNodeFolder, enforceOptions, OPEN_BROADCAST_CHANNELS } from './broadcast-channel.js'; | ||
export { createLeaderElection, beLeader } from './leader-election.js'; | ||
export { createLeaderElection } from './leader-election.js'; | ||
export { beLeader } from './leader-election-util.js'; |
Oops, something went wrong.