Skip to content

start some es6ifying #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
"use strict";

var util = require('util');
var defaultTimeout = 5000;
var promisify = util.promisify || function(x) { return x; };
const util = require('util');
const defaultTimeout = 5000;
const promisify = util.promisify || function(x) { return x; };

function acquireLock(client, lockName, timeout, retryDelay, onLockAcquired) {
function retry() {
setTimeout(function() {
/* this is where we recursively re-call ourself */
setTimeout(() => {
acquireLock(client, lockName, timeout, retryDelay, onLockAcquired);
}, retryDelay);
}

var lockTimeoutValue = (Date.now() + timeout + 1);
client.set(lockName, lockTimeoutValue, 'PX', timeout, 'NX', function(err, result) {
/* `lockTimeoutValue` is the value after which the lock is automatically released. wathever the use? */
const lockTimeoutValue = (Date.now() + timeout + /* just in case is 0 we add 1 ms */ 1);
client.set(lockName, lockTimeoutValue, 'PX', timeout, 'NX', (err, result) => {
if(err || result === null) return retry();
onLockAcquired(lockTimeoutValue);
});
}

module.exports = function(client, retryDelay) {
module.exports = (client, retryDelay) => {
if(!(client && client.setnx)) {
throw new Error("You must specify a client instance of http://github.com/mranney/node_redis");
}
Expand Down