-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfakelock.js
More file actions
65 lines (57 loc) · 1.53 KB
/
Copy pathfakelock.js
File metadata and controls
65 lines (57 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Proxies } from '../../support/proxies.js';
/**
* the lock service us meaningless here as we are running on node single threaded
* in apps script this would lock shared code that was being shared by multiple scripts
* so this is all provided for compatibility only
*/
class FakeLock {
constructor(domain) {
this.__fakeObjectType = 'Lock';
this.__domain = domain
this.__locked = false;
}
/**
* Returns true if the lock was acquired.
* @returns {boolean}
*/
hasLock() {
return this.__locked;
}
/**
* Releases the lock.
*/
releaseLock() {
this.__locked = false;
}
/**
* Attempts to acquire the lock.
* @param {number} timeoutInMillis
* @returns {boolean}
*/
tryLock(timeoutInMillis) {
if (this.hasLock()) {
return true;
}
// In a single-threaded fake, we can't wait. We fail only if timeout is negative.
if (timeoutInMillis < 0) {
return false;
}
this.__locked = true;
return true;
}
/**
* Attempts to acquire the lock, throwing an exception on timeout.
* @param {number} timeoutInMillis
*/
waitLock(timeoutInMillis) {
if (this.hasLock()) {
return; // Already acquired, no need to wait
}
// In a single-threaded fake, we can't wait. We fail only if timeout is negative.
if (timeoutInMillis < 0) {
throw new Error(`Lock timeout: another process was holding the lock for too long.`);
}
this.__locked = true;
}
}
export const newFakeLock = (...args) => Proxies.guard(new FakeLock(...args));