https://github.com/taylorhakes/localstorage-lock
Generic localstorage lock implementation
https://github.com/taylorhakes/localstorage-lock
Last synced: 6 months ago
JSON representation
Generic localstorage lock implementation
- Host: GitHub
- URL: https://github.com/taylorhakes/localstorage-lock
- Owner: taylorhakes
- License: mit
- Created: 2017-12-26T01:55:17.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-16T05:59:35.000Z (over 7 years ago)
- Last Synced: 2025-03-25T00:38:01.482Z (7 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 9
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# localstorage-lock
Generic localstorage lock implementation## API
### runWithLock
Run a specified code block with a localstorage lock**`runWithLock(, , );`**
Options:
```js
{
timeout: 1000, // Time release the lock if function fails or takes too long
lockWriteTime: 50, // Expected time to write to localstorage (unlikely to change)
checkTime: 10, // How often to recheck the lock, if don't have the lock
retry: true // Retry getting the lock, if not acquired
}
```#### Example Use
```js// Make sure only one browser window retrieves a localstorage key and does console.log
runWithLock('lock.some-key', () => {
const someKey = localStorage.getItem('some-key');
console.log(someKey)
localStorage.removeItem('some-key')
}, { timeout: 500 });
```### tryRunWithLock
Wrapper function for `tryWithLock` with option `retry: false`