Skip to content

Commit 73061e4

Browse files
authored
Merge pull request #9 from Kikobeats/next
Next
2 parents 820ec34 + 7df54d9 commit 73061e4

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/create.js

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class LinkedList {
2525
this.head = this.head.next
2626
return data
2727
}
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+
}
2838
}
2939

3040
module.exports = (slots = 1) => {
@@ -48,5 +58,7 @@ module.exports = (slots = 1) => {
4858

4959
lock.isLocked = () => slots === 0
5060

61+
lock.awaiting = () => queue.size()
62+
5163
return lock
5264
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const withLock = opts => {
1515
}
1616

1717
withLock.isLocked = lock.isLocked
18+
withLock.awaiting = lock.awaiting
1819

1920
return withLock
2021
}

test/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
})

0 commit comments

Comments
 (0)