superlock 1.3.0 → 1.3.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.
Files changed (3) hide show
  1. package/LICENSE.md +0 -0
  2. package/package.json +16 -21
  3. package/src/create.js +19 -16
package/LICENSE.md CHANGED
File without changes
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.3.0",
5
+ "version": "1.3.2",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "exports": {
@@ -14,12 +14,7 @@
14
14
  "name": "Kiko Beats",
15
15
  "url": "https://kikobeats.com"
16
16
  },
17
- "contributors": [
18
- {
19
- "name": "Tycho Bokdam",
20
- "email": "tycho@palmtreecoding.com"
21
- }
22
- ],
17
+ "contributors": [],
23
18
  "repository": {
24
19
  "type": "git",
25
20
  "url": "git+https://github.com/Kikobeats/superlock.git"
@@ -57,6 +52,19 @@
57
52
  "files": [
58
53
  "src"
59
54
  ],
55
+ "scripts": {
56
+ "clean": "rm -rf node_modules",
57
+ "contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
58
+ "coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
59
+ "lint": "standard",
60
+ "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
61
+ "prerelease": "npm run contributors",
62
+ "pretest": "npm run lint",
63
+ "release": "standard-version -a",
64
+ "release:github": "github-generate-release",
65
+ "release:tags": "git push --follow-tags origin HEAD:master",
66
+ "test": "c8 ava"
67
+ },
60
68
  "license": "MIT",
61
69
  "ava": {
62
70
  "files": [
@@ -86,18 +94,5 @@
86
94
  "simple-git-hooks": {
87
95
  "commit-msg": "npx commitlint --edit",
88
96
  "pre-commit": "npx nano-staged"
89
- },
90
- "scripts": {
91
- "clean": "rm -rf node_modules",
92
- "contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
93
- "coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
94
- "lint": "standard",
95
- "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
96
- "prerelease": "npm run contributors",
97
- "pretest": "npm run lint",
98
- "release": "standard-version -a",
99
- "release:github": "github-generate-release",
100
- "release:tags": "git push --follow-tags origin HEAD:master",
101
- "test": "c8 ava"
102
97
  }
103
- }
98
+ }
package/src/create.js CHANGED
@@ -13,20 +13,29 @@ class LinkedList {
13
13
 
14
14
  enqueue (data) {
15
15
  const node = new Node(data)
16
- if (this.head) this.tail.next = node
16
+ node.prev = this.tail
17
+ if (this.tail) this.tail.next = node
17
18
  else this.head = node
18
19
  this.tail = node
19
20
  this.length++
21
+ return node
20
22
  }
21
23
 
22
24
  dequeue () {
23
25
  if (!this.head) return
24
- const data = this.head.data
25
- this.head = this.head.next
26
- this.length--
26
+ const { data } = this.head
27
+ this.remove(this.head)
27
28
  return data
28
29
  }
29
30
 
31
+ remove (node) {
32
+ if (node.prev) node.prev.next = node.next
33
+ else this.head = node.next
34
+ if (node.next) node.next.prev = node.prev
35
+ else this.tail = node.prev
36
+ this.length--
37
+ }
38
+
30
39
  size () {
31
40
  return this.length
32
41
  }
@@ -34,15 +43,11 @@ class LinkedList {
34
43
 
35
44
  module.exports = (slots = 1) => {
36
45
  const queue = new LinkedList()
37
- let cancelled = 0
38
46
 
39
47
  const release = () => {
40
48
  ++slots
41
- let waiter
42
- while ((waiter = queue.dequeue()) !== undefined) {
43
- if (waiter.cancelled) --cancelled
44
- else return waiter.acquire()
45
- }
49
+ const waiter = queue.dequeue()
50
+ if (waiter) return waiter.acquire()
46
51
  }
47
52
 
48
53
  const acquire = resolve => {
@@ -55,12 +60,12 @@ module.exports = (slots = 1) => {
55
60
  if (signal?.aborted) return resolve(null)
56
61
  if (!lock.isLocked()) return acquire(resolve)
57
62
 
58
- const waiter = { cancelled: false, acquire: () => acquire(resolve) }
63
+ const waiter = { acquire: () => acquire(resolve) }
64
+ const node = queue.enqueue(waiter)
59
65
 
60
66
  if (signal !== undefined) {
61
67
  const onAbort = () => {
62
- waiter.cancelled = true
63
- ++cancelled
68
+ queue.remove(node)
64
69
  resolve(null)
65
70
  }
66
71
  waiter.acquire = () => {
@@ -69,13 +74,11 @@ module.exports = (slots = 1) => {
69
74
  }
70
75
  signal.addEventListener('abort', onAbort, { once: true })
71
76
  }
72
-
73
- queue.enqueue(waiter)
74
77
  })
75
78
 
76
79
  lock.isLocked = () => slots === 0
77
80
 
78
- lock.awaiting = () => queue.size() - cancelled
81
+ lock.awaiting = () => queue.size()
79
82
 
80
83
  return lock
81
84
  }