Skip to content

Commit 216dc22

Browse files
committed
fix lock
1 parent 2c1fc5f commit 216dc22

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/methods/lock.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
const Lock = () => {
2-
return {
3-
lockQueue: new Array(),
4-
locked: false
1+
class AWaitLock {
2+
constructor() {
3+
this.lockQueue = [];
4+
this.locked = false;
55
}
6-
}
76

8-
const acquire = async (lock) => {
9-
if (lock.locked) {
10-
let that = lock;
11-
await new Promise((resolve) => {
12-
that.lockQueue.push(resolve);
13-
});
7+
async acquire() {
8+
if (this.locked) {
9+
let that = this;
10+
await new Promise((resolve) => {
11+
that.lockQueue.push(resolve);
12+
});
13+
}
14+
this.locked = true;
15+
return true;
1416
}
15-
lock.locked = true;
16-
return true;
17-
};
18-
1917

20-
const release = (lock) => {
21-
lock.locked = false;
22-
const resolve = lock.lockQueue.pop();
23-
if (resolve) {
24-
resolve();
18+
release() {
19+
this.locked = false;
20+
let resolve = this.lockQueue.pop();
21+
if (resolve) {
22+
resolve();
23+
}
2524
}
2625
}
2726

28-
export {Lock, acquire, release};
27+
export default AWaitLock;

0 commit comments

Comments
 (0)