Skip to content

Commit

Permalink
chore: nodeify is a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Nov 27, 2020
1 parent 124acc4 commit 741f528
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions redlock.js
Expand Up @@ -62,14 +62,6 @@ const defaults = {
};


// MONKEYPATCH
Promise.prototype.nodeify = function (callback) {
if (typeof callback === 'function') {
this.then((v) => callback(undefined, v))
.catch((err) => callback(err));
}
return this;
};



Expand Down Expand Up @@ -160,12 +152,12 @@ Redlock.LockError = LockError;
Redlock.prototype.quit = function quit(callback) {

// quit all clients
return Promise.all(this.servers.map(function(client) {
return nodeify(Promise.all(this.servers.map(function(client) {
return client.quit();
}))
})),

// optionally run callback
.nodeify(callback);
callback);
};


Expand Down Expand Up @@ -224,7 +216,7 @@ Redlock.prototype.unlock = function unlock(lock, callback) {
// immediately invalidate the lock
lock.expiration = 0;

return new Promise(function(resolve, reject) {
return nodeify(new Promise(function(resolve, reject) {

// the number of votes needed for consensus
const quorum = Math.floor(self.servers.length / 2) + 1;
Expand Down Expand Up @@ -265,10 +257,10 @@ Redlock.prototype.unlock = function unlock(lock, callback) {
// FAILURE: the lock could not be released
return reject(new LockError('Unable to fully release the lock on resource "' + lock.resource + '".'));
}
})
}),

// optionally run callback
.nodeify(callback);
callback);
};


Expand All @@ -280,20 +272,20 @@ Redlock.prototype.extend = function extend(lock, ttl, callback) {

// the lock has expired
if(lock.expiration < Date.now())
return Promise.reject(new LockError('Cannot extend lock on resource "' + lock.resource + '" because the lock has already expired.', 0)).nodeify(callback);
return nodeify(Promise.reject(new LockError('Cannot extend lock on resource "' + lock.resource + '" because the lock has already expired.', 0)), callback);

// extend the lock
return self._lock(lock.resource, lock.value, ttl, {})
return nodeify(self._lock(lock.resource, lock.value, ttl, {})

// modify and return the original lock object
.then(function(extension){
lock.value = extension.value;
lock.expiration = extension.expiration;
return lock;
})
}),

// optionally run callback
.nodeify(callback);
callback);
};


Expand Down Expand Up @@ -340,7 +332,7 @@ Redlock.prototype._lock = function _lock(resource, value, ttl, options, callback
// array of locked resources
resource = Array.isArray(resource) ? resource : [resource];

return new Promise(function(resolve, reject) {
return nodeify(new Promise(function(resolve, reject) {
let request;

// the number of times we have attempted this lock
Expand Down Expand Up @@ -422,10 +414,10 @@ Redlock.prototype._lock = function _lock(resource, value, ttl, options, callback
}

return attempt();
})
}),

// optionally run callback
.nodeify(callback);
callback);
};


Expand All @@ -451,4 +443,12 @@ Redlock.prototype._hashScript = function(value) {
return crypto.createHash('sha1').update(value).digest('hex');
}

function nodeify(promise, callback) {
if (typeof callback === 'function') {
promise.then((v) => callback(undefined, v))
.catch((err) => callback(err));
}
return promise;
}

module.exports = Redlock;

0 comments on commit 741f528

Please sign in to comment.