aermin / web-locks Goto Github PK
View Code? Open in Web Editor NEWWeb Locks API(navigator.locks) polyfill, implement according to Specification
Home Page: https://www.npmjs.com/package/navigator.locks
License: MIT License
Web Locks API(navigator.locks) polyfill, implement according to Specification
Home Page: https://www.npmjs.com/package/navigator.locks
License: MIT License
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.
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.
Hey there, I'm running into the following error occasionally during my CI runs when running in a jsdom/happy-dom environment.
Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Uncaught Exception ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
ReferenceError: window is not defined
❯ e._setLocalTime node_modules/navigator.locks/dist/index.cjs.js:1:2930
❯ Timeout._onTimeout node_modules/navigator.locks/dist/index.cjs.js:1:2665
❯ listOnTimeout node:internal/timers:573:17
❯ processTimers node:internal/timers:514:7
This error originated in "index3.spec.js" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
This error was caught after test environment was torn down. Make sure to cancel any running tasks before test finishes:
- cancel timeouts using clearTimeout and clearInterval
- wait for promises to resolve using the await keyword
I believe this is due to the way the heartbeat file works (https://github.com/aermin/web-locks/blob/main/src/heartBeat.ts#L26 and https://github.com/aermin/web-locks/blob/main/src/polyfill.ts#L96, I've gotten stack traces that point to both of these), where it sets up intervals that access window
.
I think the root of the issue is there's a race condition between when the jsdom environment and window
get cleaned up, and when these intervals get cleaned up.
There are a couple ways I think this can be solved:
unload
handlers are called when the jsdom environment is cleaned up - they currently don't jsdom/jsdom#1584. This is also the same in happy-dom, another dom implementation used often in testing frameworks
addEventListener
for unload
event listeners and call them, the test becomes much more stableThis error happens randomly, and the code acquiring the lock just blows up. This is in Firefox 91.
👋 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();
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.