superlock 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/create.js +31 -5
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "superlock",
|
|
3
3
|
"description": "The most simple mutex/semaphore implementation",
|
|
4
4
|
"homepage": "https://github.com/Kikobeats/superlock",
|
|
5
|
-
"version": "1.1.
|
|
5
|
+
"version": "1.1.3",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"exports": {
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"@commitlint/config-conventional"
|
|
81
81
|
],
|
|
82
82
|
"rules": {
|
|
83
|
-
"body-max-length": [
|
|
83
|
+
"body-max-line-length": [
|
|
84
84
|
0
|
|
85
85
|
]
|
|
86
86
|
}
|
package/src/create.js
CHANGED
|
@@ -1,11 +1,39 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
class Node {
|
|
4
|
+
constructor (data) {
|
|
5
|
+
this.data = data
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class LinkedList {
|
|
10
|
+
enqueue (data) {
|
|
11
|
+
if (!this.head) {
|
|
12
|
+
this.head = new Node(data)
|
|
13
|
+
} else {
|
|
14
|
+
let current = this.head
|
|
15
|
+
while (current.next) {
|
|
16
|
+
current = current.next
|
|
17
|
+
}
|
|
18
|
+
current.next = new Node(data)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
dequeue () {
|
|
23
|
+
if (!this.head) return
|
|
24
|
+
const data = this.head.data
|
|
25
|
+
this.head = this.head.next
|
|
26
|
+
return data
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
3
30
|
module.exports = (slots = 1) => {
|
|
4
|
-
const queue =
|
|
31
|
+
const queue = new LinkedList()
|
|
5
32
|
|
|
6
33
|
const release = () => {
|
|
7
34
|
++slots
|
|
8
|
-
|
|
35
|
+
const fn = queue.dequeue()
|
|
36
|
+
if (fn !== undefined) fn()
|
|
9
37
|
}
|
|
10
38
|
|
|
11
39
|
const acquire = resolve => {
|
|
@@ -15,9 +43,7 @@ module.exports = (slots = 1) => {
|
|
|
15
43
|
|
|
16
44
|
const lock = () =>
|
|
17
45
|
new Promise(resolve =>
|
|
18
|
-
lock.isLocked()
|
|
19
|
-
? queue.push(acquire.bind(null, resolve))
|
|
20
|
-
: acquire(resolve)
|
|
46
|
+
lock.isLocked() ? queue.enqueue(() => acquire(resolve)) : acquire(resolve)
|
|
21
47
|
)
|
|
22
48
|
|
|
23
49
|
lock.isLocked = () => slots === 0
|