{"id":13816814,"url":"https://github.com/SlimIO/Lock","last_synced_at":"2025-05-15T18:32:34.664Z","repository":{"id":48446182,"uuid":"191788692","full_name":"SlimIO/Lock","owner":"SlimIO","description":"SlimIO Node.js Semaphore for async/await","archived":true,"fork":false,"pushed_at":"2022-09-05T16:57:03.000Z","size":31,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-31T17:09:35.896Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SlimIO.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-13T15:28:15.000Z","updated_at":"2023-04-22T19:10:20.000Z","dependencies_parsed_at":"2022-09-04T13:22:20.320Z","dependency_job_id":null,"html_url":"https://github.com/SlimIO/Lock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SlimIO%2FLock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SlimIO%2FLock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SlimIO%2FLock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SlimIO%2FLock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SlimIO","download_url":"https://codeload.github.com/SlimIO/Lock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225368845,"owners_count":17463462,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T06:00:22.062Z","updated_at":"2024-11-19T14:31:14.399Z","avatar_url":"https://github.com/SlimIO.png","language":"JavaScript","readme":"# Lock\n![version](https://img.shields.io/badge/dynamic/json.svg?url=https://raw.githubusercontent.com/SlimIO/Lock/master/package.json\u0026query=$.version\u0026label=Version)\n[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/SlimIO/Lock/commit-activity)\n![MIT](https://img.shields.io/github/license/mashape/apistatus.svg)\n![size](https://img.shields.io/bundlephobia/min/@slimio/lock)\n[![Known Vulnerabilities](https://snyk.io//test/github/SlimIO/Lock/badge.svg?targetFile=package.json)](https://snyk.io//test/github/SlimIO/Lock?targetFile=package.json)\n[![Build Status](https://travis-ci.com/SlimIO/Lock.svg?branch=master)](https://travis-ci.com/SlimIO/Lock)\n\nSlimIO Asynchronous Handler Mutex \"Like\" Lock. This package has been created to easily lock parallel execution of JavaScript Asynchronous function.\n\n## Requirements\n- [Node.js](https://nodejs.org/en/) v14 or higher\n\n## Getting Started\n\nThis package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com).\n\n```bash\n$ npm i @slimio/lock\n# or\n$ yarn add @slimio/lock\n```\n\n## Usage example\n```js\nconst Lock = require(\"@slimio/lock\");\n\nconst asyncLocker = new Lock({ maxConcurrent: 3 });\n\nasync function npmInstall() {\n    const free = await asyncLocker.acquireOne();\n    try {\n        await new Promise((resolve) =\u003e setTimeout(resolve, 1000));\n        console.log(\"npm install resolved!\");\n    }\n    finally {\n        free();\n    }\n}\n\n// Run 3 per 3 methods\nPromise.all([\n    npmInstall(),\n    npmInstall(),\n    npmInstall(),\n    npmInstall(),\n    npmInstall()\n]).then(() =\u003e console.log(\"all done!\")).catch(console.error);\n```\n\n## API\n\n### Properties\n\n```ts\ndeclare class Lock {\n    public readonly max: number;\n    public readonly running: number;\n}\n```\n\n### Methods\n\n\u003cdetails\u003e\u003csummary\u003eacquireOne(): Promise\u003c () =\u003e void \u003e\u003c/summary\u003e\n\u003cbr /\u003e\nCreate a new lock counter. Return a function that you need to execute to free the counter/lock.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003efreeOne(error?: Error): void\u003c/summary\u003e\n\u003cbr /\u003e\nfree an acquired lock (or do nothing if there is no lock acquired yet).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003erejectAll(errorMessage?: string): void\u003c/summary\u003e\n\u003cbr /\u003e\nReject all promises and available locks. This will throw an Error for each called acquireOne. The default error is `Lock acquisition rejected!`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003ereset(): void\u003c/summary\u003e\n\u003cbr /\u003e\nReset the Object (will reject if any locks or promises are detected active).\n\n\u003c/details\u003e\n\n## License\nMIT\n","funding_links":[],"categories":["Packages"],"sub_categories":["Others"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSlimIO%2FLock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSlimIO%2FLock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSlimIO%2FLock/lists"}