File tree 2 files changed +32
-6
lines changed
2 files changed +32
-6
lines changed Original file line number Diff line number Diff line change 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
}
Original file line number Diff line number Diff line change 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
- if ( queue . length > 0 ) queue . shift ( ) ( )
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
You can’t perform that action at this time.
0 commit comments