Giter Site home page Giter Site logo

aermin / web-locks Goto Github PK

View Code? Open in Web Editor NEW
52.0 4.0 4.0 330 KB

Web Locks API(navigator.locks) polyfill, implement according to Specification

Home Page: https://www.npmjs.com/package/navigator.locks

License: MIT License

JavaScript 1.02% TypeScript 98.98%
web-locks web-locks-polyfill web-locks-api-polyfill navigator-locks navigator-locks-polyfill navigator-locks-api-polyfill

web-locks's Introduction

web-locks polyfill

push workflow release workflow npm license

a Web Locks API polyfill, work when the browser does not support web lock API: navigator.lock, resolve the browser compatibility issue.

Installation

// with npm
npm install navigator.locks

// with yarn
yarn add navigator.locks

Usage

Just import It.

import "navigator.locks";

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.
});
// if you use Typescript, could import these types to use
import type {
  LockManager,
  Lock,
  LockInfo,
  LockManagerSnapshot,
  LocksInfo,
} from "navigator.locks";

navigator.locks API usage follow MDN API Doc

Online demo

demo link

You could open two tabs of this API unsupported browser, operate the lock buttons to experience

Process

All API ✅

  • navigator.locks.request

  • navigator.locks.request options:

    mode: exclusive | shared

    ifAvailable

    steal

    signal

  • navigator.locks.query

CI/CD ✅

Unit test ✅

web-locks's People

Contributors

aermin avatar dependabot[bot] avatar dlech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

web-locks's Issues

Old heartbeat items are not cleared from local storage

Hey @aermin,

Nice work on the library! I've been testing this out and noticed that for every browser tab refresh, a new item gets added to local storage.

image

I thought about a potential fix, and looking through the code I see there is an "unload" handler in heartBeat.ts that could be modified to clear the heartbeat item. However, "unload" events are not reliably fired in many browsers so might need to think of something else.

Make the library compatible with client/server environments where window is undefined

👋 Thanks for building this project.

I propose the following change to make it compatible with client/server frameworks like next.js.
At the moment trying to use the library with next.js throws the error window is not defined because a page can be rendered on the server side where window is undefined.

I tested locally and it should work with 2 simple changes

index.ts

const locks = (function () {
  // Add this block
  if (typeof window === "undefined") {
    return {
      request: async () => {},
      query: () => ({
        held: [],
        pending: [],
      }),
    } as Partial<LockManager>;
  }
  // existing code
})();

polyfill.ts

  constructor() {
    this._defaultOptions = {
      mode: LOCK_MODE.EXCLUSIVE,
      ifAvailable: false,
      steal: false,
    };
    // Wrap init inside a check
    if (typeof window !== "undefined") {
      this._init();
    }
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.