{"id":20390677,"url":"https://github.com/dizmo/functions-lock","last_synced_at":"2025-07-22T16:06:11.536Z","repository":{"id":57108668,"uuid":"396953784","full_name":"dizmo/functions-lock","owner":"dizmo","description":"dizmoFun: lock acquisition \u0026 release","archived":false,"fork":false,"pushed_at":"2022-11-16T19:21:34.000Z","size":944,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-12T00:36:32.573Z","etag":null,"topics":["acquire","dizmo","lock","release","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@dizmo/functions-lock","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dizmo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-16T20:06:14.000Z","updated_at":"2022-11-16T19:00:40.000Z","dependencies_parsed_at":"2023-01-23T11:15:42.024Z","dependency_job_id":null,"html_url":"https://github.com/dizmo/functions-lock","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/dizmo/functions-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizmo%2Ffunctions-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizmo%2Ffunctions-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizmo%2Ffunctions-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizmo%2Ffunctions-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dizmo","download_url":"https://codeload.github.com/dizmo/functions-lock/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dizmo%2Ffunctions-lock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266524894,"owners_count":23942834,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["acquire","dizmo","lock","release","typescript"],"created_at":"2024-11-15T03:26:15.396Z","updated_at":"2025-07-22T16:06:11.498Z","avatar_url":"https://github.com/dizmo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version](https://badge.fury.io/js/%40dizmo%2Ffunctions-lock.svg)](https://npmjs.org/package/@dizmo/functions-lock)\n[![Build Status](https://travis-ci.com/dizmo/functions-lock.svg?branch=master)](https://travis-ci.com/dizmo/functions-lock)\n[![Coverage Status](https://coveralls.io/repos/github/dizmo/functions-lock/badge.svg?branch=master)](https://coveralls.io/github/dizmo/functions-lock?branch=master)\n\n# @dizmo/functions-lock\n\nProvides a `Lock` class, which can be used to acquire and release (enumerated) locks \u0026ndash; where each lock can also be anonymous or named.\n\nPer lock an internal *identity context* is associated, where the corresponding data is stored as an (`ephemeral-id`, `session-id`) tuple. The `ephemeral-id` is stored in the `global` (aka the `window`) object, and the `session-id` in the `localStorage` \u0026ndash; if available, otherwise again the `global` object is used.\n\n\u003e Locks with the same name share the same identity, but each lock number is treated separately.\n\nA lock will *always* permit to be acquired, if the identity context is the same! However, this behaviour can be overridden by providing a flag to *clear* the internal identity (of a named lock), which will cause the lock to *not* be acquired \u0026ndash; if another lock with the same name as already been acquired.\n\nUpon a successful acquisition a positive number (larger than `0`) will be returned, which indicates the amount of time (in milliseconds) it took to acquire the lock; a failure to acquire it will result in a `null` object. Upon a successful release a `true` value will be returned (else the result will be `false`).\n\n## Usage\n\n### Installation\n\n```sh\nnpm install @dizmo/functions-lock --save\n```\n\n### Require\n\n```typescript\nimport { Lock } from '@dizmo/functions-lock';\n```\n\n### Example(s)\n\n#### Acquire and release lock\n```typescript\nconst lock = new Lock();\nif (await lock.acquire()) {\n    if (await lock.release()) {\n        console.debug('lock acquired and released');\n    } else {\n        console.debug('lock acquired but *not* released');\n    }\n} else {\n    console.debug('lock *not* acquired');\n}\n```\n\n#### Acquire and release lock at index=0 (default) with no expiration (default)\n```typescript\nconst lock = new Lock();\nif (await lock.acquire(0)) {\n    if (await lock.release(0)) {\n        console.debug('lock acquired @index=0 and released');\n    } else {\n        console.debug('lock acquired @index=0 but *not* released');\n    }\n} else {\n    console.debug('lock @index=0 *not* acquired');\n}\n```\n\n#### Acquire and release lock at index=0 (default) with an expiration of one minute\n```typescript\nconst lock = new Lock(), expiry_ms = 60 * 1000; // one minute\nif (await lock.acquire(0, expiry_ms)) {\n    if (await lock.release(0)) {\n        console.debug('lock acquired and released');\n    } else {\n        console.debug('lock acquired but *not* released');\n    }\n} else {\n    console.debug('lock *not* acquired');\n}\n```\n\n#### Acquire and release named lock at index=1\n```typescript\nconst lock = new Lock('my-lock');\nif (await lock.acquire(1)) {\n    if (await lock.release(1)) {\n        console.debug('named lock @index=1 acquired and released');\n    } else {\n        console.debug('named lock @index=1 acquired but *not* released');\n    }\n} else {\n    console.debug('named lock @index=1 *not* acquired');\n}\n```\n\n#### Acquire (twice) and release named lock at index=2\n```typescript\nconst lock = new Lock('my-lock');\nif (await lock.acquire(2)) {\n    if (await lock.acquire(2)) {\n        if (await lock.release(2)) {\n            console.debug('named lock @index=2 acquired and released');\n        } else {\n            console.debug('named lock @index=2 acquired but *not* released');\n        }\n    } else {\n        console.debug('named lock @index=2 *not* acquired (2nd time)');\n    }\n} else {\n    console.debug('named lock @index=2 *not* acquired (1st time)');\n}\n```\n\n#### Acquire (twice) and release named lock at index=2 with ID clearing\n```typescript\nconst lock = new Lock('my-lock', true); // clear ID i.e. \"fake\" another context!\nif (await lock.acquire(2)) {\n    if (await lock.acquire(2)) {\n        if (await lock.release(2)) {\n            console.debug('named lock @index=2 acquired and released');\n        } else {\n            console.debug('named lock @index=2 acquired but *not* released');\n        }\n    } else {\n        console.debug('named lock @index=2 *not* acquired (2nd time)');\n    }\n} else {\n    console.debug('named lock @index=2 *not* acquired (1st time)');\n}\n```\n\n## Development\n\n### Clean\n\n```sh\nnpm run clean\n```\n\n### Build\n\n```sh\nnpm run build\n```\n\n#### without linting and cleaning:\n\n```sh\nnpm run -- build --no-lint --no-clean\n```\n\n#### with UMD bundling (incl. minimization):\n\n```sh\nnpm run -- build --prepack\n```\n\n#### with UMD bundling (excl. minimization):\n\n```sh\nnpm run -- build --prepack --no-minify\n```\n\n### Lint\n\n```sh\nnpm run lint\n```\n\n#### with auto-fixing:\n\n```sh\nnpm run -- lint --fix\n```\n\n### Test\n\n```sh\nnpm run test\n```\n\n#### without linting, cleaning and (re-)building:\n\n```sh\nnpm run -- test --no-lint --no-clean --no-build\n```\n\n### Cover\n\n```sh\nnpm run cover\n```\n\n#### without linting, cleaning and (re-)building:\n\n```sh\nnpm run -- cover --no-lint --no-clean --no-build\n```\n\n## Debugging\n\nConnect `@dizmo/functions-lock` to another project:\n\n```sh\n[@dizmo/functions-lock] $ npm link # symlink global:@dizmo/functions-lock\n```\n\n```sh\n[a-project] $ npm link @dizmo/functions-lock # symlink node-modules:@dizmo/functions-lock\n```\n\n```sh\n[a-project] $ head webpack.config.js # ensure @dizmo/functions-lock in entry.main\n```\n\n```\nentry: {\n    main: [..., '@dizmo/functions-lock', './source/index.js']\n}\n```\n\nDisconnect `@dizmo/functions-lock` from the project:\n\n```sh\n[a-project] $ npm unlink @dizmo/functions-lock # delete local symlink\n```\n\n```sh\n[@dizmo/functions-lock] $ npm uninstall -g # delete global symlink\n```\n\n## Documentation\n\n```sh\nnpm run docs\n```\n\n## Publication\n\n```sh\nnpm publish\n```\n\n#### initially (if public):\n\n```sh\nnpm publish --access=public\n```\n\n## Copyright\n\n © 2021 [dizmo AG](https://dizmo.com/), Switzerland\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdizmo%2Ffunctions-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdizmo%2Ffunctions-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdizmo%2Ffunctions-lock/lists"}