npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

web-locks-polyfill

v0.0.13

Published

A polyfill for web-locks

Downloads

19,461

Readme

web-locks-polyfill

A partial polyfill for the web-locks api.

Limitations

  • This does not support an options argument to navigator.locks.request(name, [options,] callback). Thus, only the default options are supported.
  • navigator.locks.query is not currently supported.
  • This uses localStorage as a form of lock. By default, it expects these locks to only last 2 seconds. If you have a single JS task on the main window that lasts more than 2 seconds, it's possible two callbacks can run at the same time.

Setup

Quick Setup

To create a navigator.locks.request method if none exists in the browser, add the following to your HTML page:

<script src="https://unpkg.com/web-locks-polyfill/dist/web-locks-polyfill.js"></script>

See a demo of this in action. Open the previous link in 2 browser tabs. You should only see one yellow box at a time. Try it in a browser that doesn't support web-locks like Safari.

Setup with NPM

If you are using a module loader, you can install the polyfill with npm like:

npm install web-locks-polyfill

Import the default polyfill like:

import "web-locks-polyfill";

Use

Basic Use

The primary use is just like the web-locks API.

Request a lock and do something within the callback like:

navigator.locks.request('my_resource', async lock => {
  // The lock has been acquired.
  await do_something();
  await do_something_else();
  // Now the lock will be released.
});

Changing Default Configuration

You can configure your own request function by importing makeLocksRequest as follows:

import makeLocksRequest from "web-locks-polyfil/make-locks-request";

if(!navigator.locks) {
  const request = makeLocksRequest({...OPTIONS});
  navigator.locks = {
    request,
    get query(){
      throw new Error("navigator.locks.query is not implemented by this polyfill ... yet!")
    }
  };
}

makeLocksRequest takes the following options:

storage

A cross-tab storage API that will be used to read/write as a form of mutex.

outerPrefix

The outer lock key prefix used in the storageAPI

  • type: String
  • default: '_MUT_OUT_LOCK_'

innerPrefix

The inner lock key prefix used in the storageAPI

  • type: String
  • default: '_MUT_IN_LOCK_'

tick

Once a lock is requested, how many millseconds between checks. All requests for a given tab are on the same timer, and it does very little. So it can be pretty small.

  • type: Integer
  • default: 20

memorySafe

If false, the request will listen to onbeforeunloaded and onunload in an attempt to clear any locks acquired by the tab.

The polyfill sets this to false. This is because it's expected you aren't going to be building multiple request functions and need to discard them.

  • type: Boolean
  • default: true

lockTimeout

This is how long we consider a mutex-key-lock valid for.

Even with memorySafe=false, it's possible a tab crashes. In this case, we don't want to keep its mutex-key-lock locks forever. Thus, we ignore any mutex-key-locks held for longer than this time. If your application has JS that can prevent other JS from running, you should set this value greater than the maximum task JS execution time. If your JS execution time exceeds this amount, it's possible another lock request will be granted.

  • type: Integer
  • default: 2000

clientId

A clientId representing this tab. This is a randomly generated string.

  • type: string
  • default: something like "3412334521"

crossTabDebugger

A debugging utility that can combine data across tabs. The cross-tab-debugger.js file uses BroadcastChannel to see if any tab has more than 1 active lock.

  • type: {onexit, onlock, onunlock, set clientId(){}}
  • default: A mock object that does nothing.