src/lock.js
async function take(key) {
    const previousLocks = LOCKS_MAP.get(key) || [];
    let locksLength = previousLocks.length;

    log('debug', `Taking the lock on ${key} (queue length was ${locksLength})`);

    let release;

    const newLock = {
      releasePromise: new Promise(async (resolve, reject) => {
        release = resolve;

        if (LOCK_TIMEOUT !== Infinity) {
          await delay.create(LOCK_TIMEOUT);

          reject(new YError('E_LOCK_TIMEOUT'));
        }
      }),
      release,
    };

    previousLocks.push(newLock);
    locksLength++;

    if (locksLength > 1) {
      await previousLocks[locksLength - 1].releasePromise;
    } else {
      LOCKS_MAP.set(key, previousLocks);
    }
  }
async function take(key) {
    const previousLocks = LOCKS_MAP.get(key) || [];
    let locksLength = previousLocks.length;

    log('debug', `Taking the lock on ${key} (queue length was ${locksLength})`);

    let release;

    const newLock = {
      releasePromise: new Promise(async (resolve, reject) => {
        release = resolve;

        if (LOCK_TIMEOUT !== Infinity) {
          await delay.create(LOCK_TIMEOUT);

          reject(new YError('E_LOCK_TIMEOUT'));
        }
      }),
      release,
    };

    previousLocks.push(newLock);
    locksLength++;

    if (locksLength > 1) {
      await previousLocks[locksLength - 1].releasePromise;
    } else {
      LOCKS_MAP.set(key, previousLocks);
    }
  }
async function take ( key ) { const previousLocks = LOCKS_MAP . get ( key ) || [ ] ; let locksLength = previousLocks . length ; log ( 'debug' , ` ${ key } ${ locksLength } ` ) ; let release ; const newLock = { releasePromise : new Promise ( async ( resolve , reject ) => { release = resolve ; if ( LOCK_TIMEOUT !== Infinity ) { await delay . create ( LOCK_TIMEOUT ) ; reject ( new YError ( 'E_LOCK_TIMEOUT' ) ) ; } } ) , release , } ; previousLocks . push ( newLock ) ; locksLength ++ ; if ( locksLength > 1 ) { await previousLocks [ locksLength - 1 ] . releasePromise ; } else { LOCKS_MAP . set ( key , previousLocks ) ; } }
Take the lock on the given resource key
@param  {String}   key
A unique key for the locked resource
@return {Promise}
A promise to be resolved when the lock
is gained or rejected if the lock release
timeout is reached.
Take the lock on the given resource key