superlock 1.0.0 → 1.0.1

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/LICENSE.md CHANGED
File without changes
package/README.md CHANGED
@@ -32,40 +32,21 @@ const delay = require('delay')
32
32
 
33
33
  const lock = withLock()
34
34
 
35
- const promiseOne = lock.then(() => {
36
- console.log('mutual exclusion is guaranteed')
37
- return delay(50)
38
- })
39
-
40
- const promiseTwo = lock.then(() => {
41
- console.log('do something')
42
- return delay(100)
43
- })
35
+ const executions = await Promise.all(
36
+ [...Array(10).keys()].map(index =>
37
+ lock(async () => {
38
+ await delay(Math.random() * 100)
39
+ return index
40
+ })
41
+ )
42
+ )
44
43
 
45
- Promise.all([promiseOne, promiseTwo])
44
+ console.log(executions)
46
45
  ```
47
46
 
48
47
  ### as semaphore
49
48
 
50
- Just pass the desired concurrency as first argument:
51
-
52
- ```js
53
- const { withLock } = require('superlock')
54
- const delay = require('delay')
55
-
56
- const lock = withLock(2)
57
-
58
- const executions = []
59
-
60
- Promise.all(
61
- [...Array(100).keys()].map(async index => {
62
- await delay(Math.random() * 100)
63
- return index
64
- })
65
- )
66
-
67
- console.log(executions)
68
- ```
49
+ Just call `withLock(n)` being `n` the maximum of concurrency desired for the lock.
69
50
 
70
51
  ## API
71
52
 
@@ -78,7 +59,7 @@ const { withLock } = require('superlock')
78
59
 
79
60
  const lock = createLock()
80
61
 
81
- lock().then(() => {
62
+ await lock(() => {
82
63
  /* your code execution */
83
64
  })
84
65
  ```
@@ -96,7 +77,7 @@ It sets the maximum of concurrency allowed for the lock.
96
77
 
97
78
  Type: `boolean`
98
79
 
99
- It indicates if there is at least one free concurrency slots in the lock.
80
+ It returns `false` if there is at least one free concurrency slots in the lock.
100
81
 
101
82
  ## License
102
83
 
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://nicedoc.io/Kikobeats/lock",
5
- "version": "1.0.0",
5
+ "version": "1.0.1",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "josefrancisco.verdu@gmail.com",
@@ -40,7 +40,6 @@
40
40
  "prettier-standard": "latest",
41
41
  "simple-git-hooks": "latest",
42
42
  "standard": "latest",
43
- "standard-markdown": "latest",
44
43
  "standard-version": "latest"
45
44
  },
46
45
  "engines": {
@@ -49,6 +48,21 @@
49
48
  "files": [
50
49
  "src"
51
50
  ],
51
+ "scripts": {
52
+ "clean": "rm -rf node_modules",
53
+ "contributors": "(git-authors-cli && finepack --sort-ignore-object-at ava && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
54
+ "coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
55
+ "lint": "standard",
56
+ "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
57
+ "prerelease": "npm run update:check && npm run contributors",
58
+ "pretest": "npm run lint",
59
+ "release": "standard-version -a",
60
+ "release:github": "conventional-github-releaser -p angular",
61
+ "release:tags": "git push --follow-tags origin HEAD:master",
62
+ "test": "c8 ava",
63
+ "update": "ncu -u",
64
+ "update:check": "ncu -- --error-level 2"
65
+ },
52
66
  "license": "MIT",
53
67
  "ava": {
54
68
  "files": [
@@ -69,9 +83,6 @@
69
83
  "*.js,!*.min.js,": [
70
84
  "prettier-standard"
71
85
  ],
72
- "*.md": [
73
- "standard-markdown"
74
- ],
75
86
  "package.json": [
76
87
  "finepack --sort-ignore-object-at ava"
77
88
  ]
@@ -79,20 +90,5 @@
79
90
  "simple-git-hooks": {
80
91
  "commit-msg": "npx commitlint --edit",
81
92
  "pre-commit": "npx nano-staged"
82
- },
83
- "scripts": {
84
- "clean": "rm -rf node_modules",
85
- "contributors": "(git-authors-cli && finepack --sort-ignore-object-at ava && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
86
- "coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
87
- "lint": "standard-markdown README.md && standard",
88
- "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
89
- "prerelease": "npm run update:check && npm run contributors",
90
- "pretest": "npm run lint",
91
- "release": "standard-version -a",
92
- "release:github": "conventional-github-releaser -p angular",
93
- "release:tags": "git push --follow-tags origin HEAD:master",
94
- "test": "c8 ava",
95
- "update": "ncu -u",
96
- "update:check": "ncu -- --error-level 2"
97
93
  }
98
- }
94
+ }
package/src/create.js CHANGED
@@ -8,16 +8,17 @@ module.exports = (slots = 1) => {
8
8
  if (queue.length > 0) queue.shift()()
9
9
  }
10
10
 
11
- const acquire = resolve => () => {
11
+ const acquire = resolve => {
12
12
  --slots
13
13
  resolve(release)
14
14
  }
15
15
 
16
16
  const lock = () =>
17
- new Promise(resolve => {
18
- const fn = acquire(resolve)
19
- lock.isLocked() ? queue.push(fn) : fn()
20
- })
17
+ new Promise(resolve =>
18
+ lock.isLocked()
19
+ ? queue.push(acquire.bind(null, resolve))
20
+ : acquire(resolve)
21
+ )
21
22
 
22
23
  lock.isLocked = () => slots === 0
23
24