File tree 3 files changed +41
-0
lines changed
3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,16 @@ class LinkedList {
25
25
this . head = this . head . next
26
26
return data
27
27
}
28
+
29
+ size ( ) {
30
+ let count = 0
31
+ let current = this . head
32
+ while ( current ) {
33
+ count ++
34
+ current = current . next
35
+ }
36
+ return count
37
+ }
28
38
}
29
39
30
40
module . exports = ( slots = 1 ) => {
@@ -48,5 +58,7 @@ module.exports = (slots = 1) => {
48
58
49
59
lock . isLocked = ( ) => slots === 0
50
60
61
+ lock . awaiting = ( ) => queue . size ( )
62
+
51
63
return lock
52
64
}
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ const withLock = opts => {
15
15
}
16
16
17
17
withLock . isLocked = lock . isLocked
18
+ withLock . awaiting = lock . awaiting
18
19
19
20
return withLock
20
21
}
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const { createLock } = require ( 'superlock' )
4
+ const test = require ( 'ava' )
5
+
6
+ test ( '.awaiting' , async t => {
7
+ const n = 100
8
+
9
+ const lock = createLock ( 1 )
10
+
11
+ t . is ( lock . isLocked ( ) , false )
12
+
13
+ const collection = [ ...Array ( n ) . keys ( ) ]
14
+
15
+ const promise = Promise . all (
16
+ collection . map ( async index => {
17
+ const release = await lock ( )
18
+ release ( )
19
+ return index
20
+ } )
21
+ )
22
+
23
+ t . is ( lock . awaiting ( ) , 99 )
24
+
25
+ await promise
26
+
27
+ t . is ( lock . awaiting ( ) , 0 )
28
+ } )
You can’t perform that action at this time.
0 commit comments