Skip to content

Commit 2c1fc5f

Browse files
committed
add lock
1 parent 8598c9d commit 2c1fc5f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/methods/lock.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Lock = () => {
2+
return {
3+
lockQueue: new Array(),
4+
locked: false
5+
}
6+
}
7+
8+
const acquire = async (lock) => {
9+
if (lock.locked) {
10+
let that = lock;
11+
await new Promise((resolve) => {
12+
that.lockQueue.push(resolve);
13+
});
14+
}
15+
lock.locked = true;
16+
return true;
17+
};
18+
19+
20+
const release = (lock) => {
21+
lock.locked = false;
22+
const resolve = lock.lockQueue.pop();
23+
if (resolve) {
24+
resolve();
25+
}
26+
}
27+
28+
export {Lock, acquire, release};

0 commit comments

Comments
 (0)