{"id":20244339,"url":"https://github.com/protonmail/mutex-browser","last_synced_at":"2025-04-10T20:45:05.110Z","repository":{"id":44645821,"uuid":"141436081","full_name":"ProtonMail/mutex-browser","owner":"ProtonMail","description":"Acquire a mutex in the browser through IndexedDB or cookies","archived":false,"fork":false,"pushed_at":"2022-02-02T11:54:36.000Z","size":62,"stargazers_count":23,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T18:21:12.383Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/ProtonMail.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":"2018-07-18T13:07:15.000Z","updated_at":"2024-10-09T14:38:35.000Z","dependencies_parsed_at":"2022-09-18T17:50:19.656Z","dependency_job_id":null,"html_url":"https://github.com/ProtonMail/mutex-browser","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProtonMail%2Fmutex-browser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProtonMail%2Fmutex-browser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProtonMail%2Fmutex-browser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProtonMail%2Fmutex-browser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ProtonMail","download_url":"https://codeload.github.com/ProtonMail/mutex-browser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248294633,"owners_count":21079939,"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-14T09:14:37.613Z","updated_at":"2025-04-10T20:45:05.078Z","avatar_url":"https://github.com/ProtonMail.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mutex-browser\n\nThis library provides mutual exclusion functionality for the browser. It supports two methods:\n\n1. Synchronizing mutex through the fast-mutex algorithm backed by cookie storage.\n2. Synchronizing mutex through the transactional guarantees of IndexedDB.\n\nThe mutex backed by a cookie storage can be useful where IndexedDB or localStorage can not be used, for instance through cross-domain cross-window synchronization. For a single domain where IndexedDB can be used, the second method is recommended.\n\nThe library requires the support of Promises, async/await, modules, and cookies or IndexedDB.\n\n## Browser support\n\nChrome, Safari, Firefox, Edge, IE11\n\n## Usage\n\n```javascript\nimport { createIDBMutex, createCookieMutex } from 'mutex-browser'\n\nconst options = {\n    expiry: 1000\n};\nconst mutex = createCookieMutex(options) or createIDBMutex(options)\n\nconst synchronized = async () =\u003e {\n    await mutex.lock('name')\n    // perform work\n    await mutex.unlock('name')\n}\n```\n\n## Default Options\n\n```javascript\nconst options = {\n    expiry: 10000, // Max time in ms before the lock will expire. Note: The function can't take longer than this.\n    spinTimeout: 20, // The time in ms before with how long the retry should spin. Note: This will be randomized to prevent starving.\n    id: 'random-uid', // The id of mutex contender. Must be unique.\n\n    // for the cookie lock\n    keyX: (name) =\u003e `${name}_lock_x`, // A function for the name to give to the key X\n    keyY: (name) =\u003e `${name}_lock_y`, // A function for the name to give to the key Y\n\n    // for the IndexedDB lock\n    objectStoreName: 'mutex', // The name of the IndexedDB store.\n    dbName: 'mutex', // The name of the IndexedDB database.\n};\n```\n\n## Example\n\nExample available in the example/ folder\n\n## Credits\n\nIndexedDB lock based on the work of Robert Knight https://github.com/robertknight/idb-mutex.\n\n## Author\n\nMattias Svanström (@mmso) - ProtonMail\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprotonmail%2Fmutex-browser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprotonmail%2Fmutex-browser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprotonmail%2Fmutex-browser/lists"}