Skip to content

Commit

Permalink
4.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Oct 29, 2021
1 parent b733224 commit 6bb6bf5
Show file tree
Hide file tree
Showing 39 changed files with 1,026 additions and 632 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## X.X.X (comming soon)

## 4.3.0 (30 October 2021)

Features:
- Added `LeaderElector.hasLeader`
- Added `LeaderElector.broadcastChannel`
Expand Down
8 changes: 4 additions & 4 deletions dist/es5node/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.BroadcastChannel = void 0;
exports.clearNodeFolder = clearNodeFolder;
exports.enforceOptions = enforceOptions;
exports.BroadcastChannel = void 0;

var _util = require("./util.js");

Expand Down Expand Up @@ -85,7 +85,7 @@ function clearNodeFolder(options) {
return true;
});
} else {
return Promise.resolve(false);
return _util.PROMISE_RESOLVED_FALSE;
}
}
/**
Expand Down Expand Up @@ -155,7 +155,7 @@ BroadcastChannel.prototype = {
}

this.closed = true;
var awaitPrepare = this._prepP ? this._prepP : Promise.resolve();
var awaitPrepare = this._prepP ? this._prepP : _util.PROMISE_RESOLVED_VOID;
this._onML = null;
this._addEL.message = [];
return awaitPrepare // wait until all current sending are processed
Expand Down Expand Up @@ -193,7 +193,7 @@ function _post(broadcastChannel, type, msg) {
type: type,
data: msg
};
var awaitPrepare = broadcastChannel._prepP ? broadcastChannel._prepP : Promise.resolve();
var awaitPrepare = broadcastChannel._prepP ? broadcastChannel._prepP : _util.PROMISE_RESOLVED_VOID;
return awaitPrepare.then(function () {
var sendPromise = broadcastChannel.method.postMessage(broadcastChannel._state, msgObj); // add/remove to unsend messages list

Expand Down
12 changes: 6 additions & 6 deletions dist/es5node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ Object.defineProperty(exports, "BroadcastChannel", {
return _broadcastChannel.BroadcastChannel;
}
});
Object.defineProperty(exports, "clearNodeFolder", {
Object.defineProperty(exports, "beLeader", {
enumerable: true,
get: function get() {
return _broadcastChannel.clearNodeFolder;
return _leaderElection.beLeader;
}
});
Object.defineProperty(exports, "enforceOptions", {
Object.defineProperty(exports, "clearNodeFolder", {
enumerable: true,
get: function get() {
return _broadcastChannel.enforceOptions;
return _broadcastChannel.clearNodeFolder;
}
});
Object.defineProperty(exports, "createLeaderElection", {
Expand All @@ -27,10 +27,10 @@ Object.defineProperty(exports, "createLeaderElection", {
return _leaderElection.createLeaderElection;
}
});
Object.defineProperty(exports, "beLeader", {
Object.defineProperty(exports, "enforceOptions", {
enumerable: true,
get: function get() {
return _leaderElection.beLeader;
return _broadcastChannel.enforceOptions;
}
});

Expand Down
66 changes: 47 additions & 19 deletions dist/es5node/leader-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ var _util = require("./util.js");

var _unload = _interopRequireDefault(require("unload"));

var LeaderElection = function LeaderElection(channel, options) {
this._channel = channel;
var LeaderElection = function LeaderElection(broadcastChannel, options) {
this.broadcastChannel = broadcastChannel;
this._options = options;
this.isLeader = false;
this.hasLeader = false;
this.isDead = false;
this.token = (0, _util.randomToken)();
this._isApl = false; // _isApplying
Expand All @@ -38,12 +39,18 @@ LeaderElection.prototype = {
applyOnce: function applyOnce() {
var _this = this;

if (this.isLeader) return Promise.resolve(false);
if (this.isDead) return Promise.resolve(false); // do nothing if already running
if (this.isLeader) {
return _util.PROMISE_RESOLVED_FALSE;
}

if (this.isDead) {
return _util.PROMISE_RESOLVED_FALSE;
} // do nothing if already running


if (this._isApl) {
this._reApply = true;
return Promise.resolve(false);
return _util.PROMISE_RESOLVED_FALSE;
}

this._isApl = true;
Expand All @@ -65,23 +72,32 @@ LeaderElection.prototype = {
if (msg.action === 'tell') {
// other is already leader
stopCriteria = true;
_this.hasLeader = true;
}
}
};

this._channel.addEventListener('internal', handleMessage);
this.broadcastChannel.addEventListener('internal', handleMessage);

var ret = _sendMessage(this, 'apply') // send out that this one is applying
.then(function () {
return (0, _util.sleep)(_this._options.responseTime);
}) // let others time to respond
.then(function () {
if (stopCriteria) return Promise.reject(new Error());else return _sendMessage(_this, 'apply');
if (stopCriteria) {
return _util.PROMISE_REJECTED;
} else {
return _sendMessage(_this, 'apply');
}
}).then(function () {
return (0, _util.sleep)(_this._options.responseTime);
}) // let others time to respond
.then(function () {
if (stopCriteria) return Promise.reject(new Error());else return _sendMessage(_this);
if (stopCriteria) {
return _util.PROMISE_REJECTED;
} else {
return _sendMessage(_this);
}
}).then(function () {
return beLeader(_this);
}) // no one disagreed -> this one is now leader
Expand All @@ -91,7 +107,7 @@ LeaderElection.prototype = {
return false;
}) // apply not successfull
.then(function (success) {
_this._channel.removeEventListener('internal', handleMessage);
_this.broadcastChannel.removeEventListener('internal', handleMessage);

_this._isApl = false;

Expand Down Expand Up @@ -120,11 +136,15 @@ LeaderElection.prototype = {
die: function die() {
var _this2 = this;

if (this.isDead) return;
if (this.isDead || !this.isLeader) {
return;
}

this.hasLeader = false;
this.isDead = true;

this._lstns.forEach(function (listener) {
return _this2._channel.removeEventListener('internal', listener);
return _this2.broadcastChannel.removeEventListener('internal', listener);
});

this._invs.forEach(function (interval) {
Expand All @@ -138,9 +158,15 @@ LeaderElection.prototype = {
return _sendMessage(this, 'death');
}
};
/**
* @param leaderElector {LeaderElector}
*/

function _awaitLeadershipOnce(leaderElector) {
if (leaderElector.isLeader) return Promise.resolve();
if (leaderElector.isLeader) {
return _util.PROMISE_RESOLVED_VOID;
}

return new Promise(function (res) {
var resolved = false;

Expand All @@ -151,9 +177,7 @@ function _awaitLeadershipOnce(leaderElector) {

resolved = true;
clearInterval(interval);

leaderElector._channel.removeEventListener('internal', whenDeathListener);

leaderElector.broadcastChannel.removeEventListener('internal', whenDeathListener);
res(true);
} // try once now

Expand All @@ -177,13 +201,16 @@ function _awaitLeadershipOnce(leaderElector) {

var whenDeathListener = function whenDeathListener(msg) {
if (msg.context === 'leader' && msg.action === 'death') {
leaderElector.hasLeader = false;
leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
});
}
};

leaderElector._channel.addEventListener('internal', whenDeathListener);
leaderElector.broadcastChannel.addEventListener('internal', whenDeathListener);

leaderElector._lstns.push(whenDeathListener);
});
Expand All @@ -199,11 +226,12 @@ function _sendMessage(leaderElector, action) {
action: action,
token: leaderElector.token
};
return leaderElector._channel.postInternal(msgJson);
return leaderElector.broadcastChannel.postInternal(msgJson);
}

function beLeader(leaderElector) {
leaderElector.isLeader = true;
leaderElector.hasLeader = true;

var unloadFn = _unload["default"].add(function () {
return leaderElector.die();
Expand Down Expand Up @@ -235,7 +263,7 @@ function beLeader(leaderElector) {
}
};

leaderElector._channel.addEventListener('internal', isLeaderListener);
leaderElector.broadcastChannel.addEventListener('internal', isLeaderListener);

leaderElector._lstns.push(isLeaderListener);

Expand Down
30 changes: 16 additions & 14 deletions dist/es5node/methods/indexed-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getIdb = getIdb;
exports.averageResponseTime = averageResponseTime;
exports.canBeUsed = canBeUsed;
exports.cleanOldMessages = cleanOldMessages;
exports.close = close;
exports.create = create;
exports.createDatabase = createDatabase;
exports.writeMessage = writeMessage;
exports["default"] = void 0;
exports.getAllMessages = getAllMessages;
exports.getIdb = getIdb;
exports.getMessagesHigherThan = getMessagesHigherThan;
exports.removeMessageById = removeMessageById;
exports.getOldMessages = getOldMessages;
exports.cleanOldMessages = cleanOldMessages;
exports.create = create;
exports.close = close;
exports.postMessage = postMessage;
exports.microSeconds = void 0;
exports.onMessage = onMessage;
exports.canBeUsed = canBeUsed;
exports.averageResponseTime = averageResponseTime;
exports["default"] = exports.type = exports.microSeconds = void 0;
exports.postMessage = postMessage;
exports.removeMessageById = removeMessageById;
exports.type = void 0;
exports.writeMessage = writeMessage;

var _util = require("../util.js");

Expand Down Expand Up @@ -215,7 +217,7 @@ function create(channelName, options) {
*/
eMIs: new _obliviousSet.ObliviousSet(options.idb.ttl * 2),
// ensures we do not read messages in parrallel
writeBlockPromise: Promise.resolve(),
writeBlockPromise: _util.PROMISE_RESOLVED_VOID,
messagesCallback: null,
readQueuePromises: [],
db: db
Expand Down Expand Up @@ -269,9 +271,9 @@ function _filterMessage(msgObj, state) {

function readNewMessages(state) {
// channel already closed
if (state.closed) return Promise.resolve(); // if no one is listening, we do not need to scan for new messages
if (state.closed) return _util.PROMISE_RESOLVED_VOID; // if no one is listening, we do not need to scan for new messages

if (!state.messagesCallback) return Promise.resolve();
if (!state.messagesCallback) return _util.PROMISE_RESOLVED_VOID;
return getMessagesHigherThan(state.db, state.lastCursorId).then(function (newerMessages) {
var useMessages = newerMessages
/**
Expand Down Expand Up @@ -299,7 +301,7 @@ function readNewMessages(state) {
state.messagesCallback(msgObj.data);
}
});
return Promise.resolve();
return _util.PROMISE_RESOLVED_VOID;
});
}

Expand Down
18 changes: 10 additions & 8 deletions dist/es5node/methods/localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getLocalStorage = getLocalStorage;
exports.storageKey = storageKey;
exports.postMessage = postMessage;
exports.addStorageEventListener = addStorageEventListener;
exports.removeStorageEventListener = removeStorageEventListener;
exports.create = create;
exports.averageResponseTime = averageResponseTime;
exports.canBeUsed = canBeUsed;
exports.close = close;
exports.create = create;
exports["default"] = void 0;
exports.getLocalStorage = getLocalStorage;
exports.microSeconds = void 0;
exports.onMessage = onMessage;
exports.canBeUsed = canBeUsed;
exports.averageResponseTime = averageResponseTime;
exports["default"] = exports.type = exports.microSeconds = void 0;
exports.postMessage = postMessage;
exports.removeStorageEventListener = removeStorageEventListener;
exports.storageKey = storageKey;
exports.type = void 0;

var _obliviousSet = require("oblivious-set");

Expand Down
13 changes: 7 additions & 6 deletions dist/es5node/methods/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.create = create;
exports.averageResponseTime = averageResponseTime;
exports.canBeUsed = canBeUsed;
exports.close = close;
exports.postMessage = postMessage;
exports.create = create;
exports.microSeconds = exports["default"] = void 0;
exports.onMessage = onMessage;
exports.canBeUsed = canBeUsed;
exports.averageResponseTime = averageResponseTime;
exports["default"] = exports.type = exports.microSeconds = void 0;
exports.postMessage = postMessage;
exports.type = void 0;

var _util = require("../util");

Expand Down Expand Up @@ -43,7 +44,7 @@ function close(channelState) {
function postMessage(channelState, messageJson) {
try {
channelState.bc.postMessage(messageJson, false);
return Promise.resolve();
return _util.PROMISE_RESOLVED_VOID;
} catch (err) {
return Promise.reject(err);
}
Expand Down
Loading

0 comments on commit 6bb6bf5

Please sign in to comment.