localstorage-lock

1.0.1 • Public • Published

localstorage-lock

Generic localstorage lock implementation

API

runWithLock

Run a specified code block with a localstorage lock

runWithLock(<localstorage key>, <fn to run>, <options>);

Options:

{
  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

 
// 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

/localstorage-lock/

    Package Sidebar

    Install

    npm i localstorage-lock

    Weekly Downloads

    977

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    36.8 kB

    Total Files

    16

    Last publish

    Collaborators

    • taylorhakes