Guest User

Untitled

a guest
Oct 12th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. /**
  2. * Locks this item.
  3. * @param level Whether lock is share or exclusive.
  4. * @param isDeep Whether lock is deep.
  5. * @param requestedTimeOut Lock timeout which was requested by client.
  6. * Server may ignore this parameter and set any timeout.
  7. * @param owner Owner of the lock as specified by client.
  8. * @returns
  9. * Instance of @see LockResult with information about the lock.
  10. */
  11. public async lock(
  12. level: LockLevel | null,
  13. isDeep: boolean | null,
  14. requestedTimeOut: number | null,
  15. owner: string | null
  16. ): Promise<LockResult> {
  17. await this.requireUnlocked(level === LockLevel.shared);
  18. const token = randomBytes(16).toString('hex');
  19. // If timeout is absent or infinit timeout requested,
  20. // grant 5 minute lock.
  21. let timeOut = 5 * 60 * 1000;
  22. if (requestedTimeOut && (requestedTimeOut < 8640000000000000)) {
  23. timeOut = requestedTimeOut;
  24. }
  25.  
  26. const lockInfo = new DateLockInfo();
  27. lockInfo.expiration = Date.now() + timeOut;
  28. lockInfo.isDeep = false;
  29. lockInfo.level = level !== null ? level : LockLevel.shared;
  30. lockInfo.lockRoot = this.path;
  31. lockInfo.lockToken = token;
  32. lockInfo.clientOwner = owner || '';
  33. lockInfo.timeOut = timeOut;
  34.  
  35. this.saveLock(lockInfo);
  36.  
  37. return new LockResult(lockInfo.lockToken, lockInfo.timeOut);
  38. }
Add Comment
Please, Sign In to add comment