{"id":20192046,"url":"https://github.com/rocicorp/lock","last_synced_at":"2025-04-10T09:52:28.339Z","repository":{"id":57683599,"uuid":"469881800","full_name":"rocicorp/lock","owner":"rocicorp","description":"Provides Lock and RwLock synchronization primitives.","archived":false,"fork":false,"pushed_at":"2024-12-09T22:30:29.000Z","size":143,"stargazers_count":14,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-02T15:05:52.021Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@rocicorp/lock","language":"TypeScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rocicorp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-14T19:45:49.000Z","updated_at":"2024-12-19T00:35:15.000Z","dependencies_parsed_at":"2024-06-03T15:11:41.653Z","dependency_job_id":"ab643149-ffa2-40ff-91cb-41923f16f029","html_url":"https://github.com/rocicorp/lock","commit_stats":{"total_commits":18,"total_committers":3,"mean_commits":6.0,"dds":"0.16666666666666663","last_synced_commit":"1571bd19a64c7f68eba140313cc7f599ebcaadb7"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocicorp%2Flock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rocicorp","download_url":"https://codeload.github.com/rocicorp/lock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248198159,"owners_count":21063625,"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-11-14T04:00:40.679Z","updated_at":"2025-04-10T09:52:28.313Z","avatar_url":"https://github.com/rocicorp.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lock\n\nProvides `Lock` and `RWLock` (read write lock) synchronization primitives for\nprotecting in-memory state across multiple tasks and/or microtasks.\n\n# Installation\n\n```\nnpm install @rocicorp/lock\n```\n\n# Usage\n\n`Lock` is a mutex that can be used to synchronize access to a shared resource.\n\n```ts\nimport {Lock} from '@rocicorp/lock';\n\nconst lock = new Lock();\n\nasync function f(n) {\n  const v = await lock.withLock(async () =\u003e {\n    await sleep(1000);\n    return n;\n  });\n  console.log(n);\n}\n\nvoid f(1);\nvoid f(2);\n// prints 1 at t=1000\n// prints 2 at t=2000\n```\n\n`RWLock` is a read write lock. There can be mutlipe readers at the same time but only one writer at the same time.\n\n```js\nimport {RWLock} from '@rocicorp/lock';\n\nconst rwLock = new RWLock();\n\nasync function read(n) {\n  const v = await lock.withRead(async () =\u003e {\n    await sleep(1000);\n    return n;\n  });\n  console.log('read', n);\n}\n\nasync function write(n) {\n  const v = await lock.withWrite(async () =\u003e {\n    await sleep(1000);\n    return n;\n  });\n  console.log('write', n);\n}\n\nvoid read(1);\nvoid write(2);\nvoid read(3);\n// prints read 1 at t=1000\n// prints read 3 at t=1000\n// prints write 2 at t=2000\n```\n\nBoth `Lock` and `RWLock` expose non \"with\" methods (`lock`, `read` and `write`). These returns a promise to a release function that resolves when the lock is acquired. This is useful when you cannot wrap your code in a function like the examples above. When using these For example:\n\n```js\nconst lock = new Lock();\n\nconst release = await lock.lock();\n// here we got the lock\n// do something\nrelease();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocicorp%2Flock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frocicorp%2Flock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocicorp%2Flock/lists"}