superlock 0.0.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2022 Kiko Beats <josefrancisco.verdu@gmail.com> (kikobeats.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ ![Last version](https://img.shields.io/github/tag/superlock.svg?style=flat-square)
2
+ [![Coverage Status](https://img.shields.io/coveralls/superlock.svg?style=flat-square)](https://coveralls.io/github/superlock)
3
+ [![NPM Status](https://img.shields.io/npm/dm/superlock.svg?style=flat-square)](https://www.npmjs.org/package/superlock)
4
+
5
+ > A mutex/semaphore implementation made easy to use.
6
+
7
+ ## Why
8
+
9
+ **superlock** aims to be:
10
+
11
+ - **Simple**: Designed for usage with `async` and `await`
12
+ - **Powerful**: Mutex & Semaphore patterns supported
13
+ - **Secure**: Auto lock release toa void dead locks
14
+ - **Lightweight**: No dependencies, just ~50 LOC
15
+ - **Well-tested**: 100% code coverage
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ $ npm install superlock --save
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### as mutex
26
+
27
+ The lock is a mutex by default:
28
+
29
+ ```js
30
+ const { withLock } = require('superlock')
31
+ const delay = require('delay')
32
+
33
+ const lock = withLock()
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
+ })
44
+
45
+ Promise.all([promiseOne, promiseTwo])
46
+ ```
47
+
48
+ ### as semaphore
49
+
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
+ ```
69
+
70
+ ## API
71
+
72
+ ### withLock([concurrency=1])
73
+
74
+ It returns a function that can be used to wrap any code you want to execute with concurrency control:
75
+
76
+ ```js
77
+ const { withLock } = require('superlock')
78
+
79
+ const lock = createLock()
80
+
81
+ lock().then(() => {
82
+ /* your code execution */
83
+ })
84
+ ```
85
+
86
+ The lock will be automatically released after your code execution is done even if an error occurred, avoiding [deadlock](https://en.wikipedia.org/wiki/Deadlock) situations.
87
+
88
+ #### concurrency
89
+
90
+ Type: `number`<br>
91
+ Default: `1`
92
+
93
+ It sets the maximum of concurrency allowed for the lock.
94
+
95
+ ### .isLocked()
96
+
97
+ Type: `boolean`
98
+
99
+ It indicates if there is at least one free concurrency slots in the lock.
100
+
101
+ ## License
102
+
103
+ **superlock** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/lock/blob/master/LICENSE.md) License.<br>
104
+ Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/lock/contributors).
105
+
106
+ > [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats)
package/package.json ADDED
@@ -0,0 +1,97 @@
1
+ {
2
+ "name": "superlock",
3
+ "description": "The most simple mutex/semaphore implementation",
4
+ "homepage": "https://nicedoc.io/Kikobeats/lock",
5
+ "version": "0.0.0",
6
+ "main": "src/index.js",
7
+ "author": {
8
+ "email": "josefrancisco.verdu@gmail.com",
9
+ "name": "Kiko Beats",
10
+ "url": "https://kikobeats.com"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/Kikobeats/lock.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/Kikobeats/lock/issues"
18
+ },
19
+ "keywords": [
20
+ "async",
21
+ "await",
22
+ "concurrency",
23
+ "critical",
24
+ "lock",
25
+ "mutex",
26
+ "section",
27
+ "semaphore"
28
+ ],
29
+ "devDependencies": {
30
+ "@commitlint/cli": "latest",
31
+ "@commitlint/config-conventional": "latest",
32
+ "ava": "latest",
33
+ "c8": "latest",
34
+ "conventional-github-releaser": "latest",
35
+ "finepack": "latest",
36
+ "git-authors-cli": "latest",
37
+ "nano-staged": "latest",
38
+ "npm-check-updates": "latest",
39
+ "prettier-standard": "latest",
40
+ "simple-git-hooks": "latest",
41
+ "standard": "latest",
42
+ "standard-markdown": "latest",
43
+ "standard-version": "latest"
44
+ },
45
+ "engines": {
46
+ "node": ">= 14"
47
+ },
48
+ "files": [
49
+ "src"
50
+ ],
51
+ "license": "MIT",
52
+ "ava": {
53
+ "files": [
54
+ "!test/helpers.js",
55
+ "test/**/*.js"
56
+ ]
57
+ },
58
+ "commitlint": {
59
+ "extends": [
60
+ "@commitlint/config-conventional"
61
+ ]
62
+ },
63
+ "exports": {
64
+ ".": "./src/index.js",
65
+ "./create": "./src/create.js"
66
+ },
67
+ "nano-staged": {
68
+ "*.js,!*.min.js,": [
69
+ "prettier-standard"
70
+ ],
71
+ "*.md": [
72
+ "standard-markdown"
73
+ ],
74
+ "package.json": [
75
+ "finepack"
76
+ ]
77
+ },
78
+ "simple-git-hooks": {
79
+ "commit-msg": "npx commitlint --edit",
80
+ "pre-commit": "npx nano-staged"
81
+ },
82
+ "scripts": {
83
+ "clean": "rm -rf node_modules",
84
+ "contributors": "(git-authors-cli && finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
85
+ "coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
86
+ "lint": "standard-markdown README.md && standard",
87
+ "postrelease": "npm run release:tags && npm run release:github && npm publish",
88
+ "prerelease": "npm run update:check && npm run contributors",
89
+ "pretest": "npm run lint",
90
+ "release": "standard-version -a",
91
+ "release:github": "conventional-github-releaser -p angular",
92
+ "release:tags": "git push --follow-tags origin HEAD:master",
93
+ "test": "c8 ava",
94
+ "update": "ncu -u",
95
+ "update:check": "ncu -- --error-level 2"
96
+ }
97
+ }
package/src/create.js ADDED
@@ -0,0 +1,34 @@
1
+ 'use strict'
2
+
3
+ module.exports = (slots = 1) => {
4
+ const queue = []
5
+
6
+ const release = () => {
7
+ ++slots
8
+ if (queue.length > 0) queue.shift()()
9
+ }
10
+
11
+ const acquire = resolve => () => {
12
+ --slots
13
+ resolve(release)
14
+ }
15
+
16
+ const lock = () =>
17
+ new Promise(resolve => {
18
+ const fn = acquire(resolve)
19
+ lock.isLocked() ? queue.push(fn) : fn()
20
+ })
21
+
22
+ lock.withLock = async fn => {
23
+ const release = await lock()
24
+ try {
25
+ return await fn()
26
+ } finally {
27
+ release()
28
+ }
29
+ }
30
+
31
+ lock.isLocked = () => slots === 0
32
+
33
+ return lock
34
+ }
package/src/index.js ADDED
@@ -0,0 +1,22 @@
1
+ 'use strict'
2
+
3
+ const createLock = require('./create')
4
+
5
+ const withLock = opts => {
6
+ const lock = createLock(opts)
7
+
8
+ const withLock = async fn => {
9
+ const release = await lock()
10
+ try {
11
+ return await fn()
12
+ } finally {
13
+ release()
14
+ }
15
+ }
16
+
17
+ withLock.isLocked = lock.isLocked
18
+
19
+ return withLock
20
+ }
21
+
22
+ module.exports = { withLock, createLock }