superlock 1.1.0 → 1.1.2

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 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.0",
5
+ "version": "1.1.2",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "exports": {
@@ -78,7 +78,12 @@
78
78
  "commitlint": {
79
79
  "extends": [
80
80
  "@commitlint/config-conventional"
81
- ]
81
+ ],
82
+ "rules": {
83
+ "body-max-line-length": [
84
+ 0
85
+ ]
86
+ }
82
87
  },
83
88
  "nano-staged": {
84
89
  "*.js": [
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
- 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
package/src/index.d.ts CHANGED
@@ -14,6 +14,6 @@ interface WithLockInterface extends WithLock {
14
14
  isLocked: () => boolean;
15
15
  }
16
16
 
17
- declare function withLock(opts: number): WithLockInterface;
17
+ declare function withLock(opts?: number): WithLockInterface;
18
18
 
19
19
  export { withLock, createLock };