From f5cf79c3ac4dd57f0ffc0f41ec7576797ab23e7c Mon Sep 17 00:00:00 2001 From: Jon Maim Date: Fri, 24 Jan 2020 20:30:12 +0530 Subject: [PATCH 1/2] start some es6ifying --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7558e48..3081860 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,17 @@ "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() { + setTimeout(() => { acquireLock(client, lockName, timeout, retryDelay, onLockAcquired); }, retryDelay); } - var lockTimeoutValue = (Date.now() + timeout + 1); + const lockTimeoutValue = (Date.now() + timeout + 1); client.set(lockName, lockTimeoutValue, 'PX', timeout, 'NX', function(err, result) { if(err || result === null) return retry(); onLockAcquired(lockTimeoutValue); From 70c4c9758a4fcb21c6870f6a8c51ab9db9577c37 Mon Sep 17 00:00:00 2001 From: Jon Maim Date: Fri, 24 Jan 2020 20:40:46 +0530 Subject: [PATCH 2/2] e6ify and comments --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3081860..2378459 100644 --- a/index.js +++ b/index.js @@ -6,19 +6,21 @@ const promisify = util.promisify || function(x) { return x; }; function acquireLock(client, lockName, timeout, retryDelay, onLockAcquired) { function retry() { + /* this is where we recursively re-call ourself */ setTimeout(() => { acquireLock(client, lockName, timeout, retryDelay, onLockAcquired); }, retryDelay); } - const 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"); }