Skip to content

Commit

Permalink
Merge pull request #2 from Kikobeats/next
Browse files Browse the repository at this point in the history
perf: avoid function
  • Loading branch information
Kikobeats committed Nov 21, 2022
2 parents 0e7b26a + 86d5a96 commit 2b1b29f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ module.exports = (slots = 1) => {
if (queue.length > 0) queue.shift()()
}

const acquire = resolve => () => {
const acquire = resolve => {
--slots
resolve(release)
}

const lock = () =>
new Promise(resolve => {
const fn = acquire(resolve)
lock.isLocked() ? queue.push(fn) : fn()
})
new Promise(resolve =>
lock.isLocked()
? queue.push(acquire.bind(null, resolve))
: acquire(resolve)
)

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

Expand Down

0 comments on commit 2b1b29f

Please sign in to comment.