bitovi / bitovi/web-locks-polyfill
old double-lock code
- Dominant language
- JavaScript
- Stars
- 3
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
```js
function progressLockState_double_lock(lockRequest, now) {
const elapsedTime = now - lockRequest.requestStart;
if (elapsedTime >= timeout) {
console.log(new Date().getTime(), lockRequest.id, "timed-out!");
lockRequest.state = "timed-out";
removeItem(lockRequest.innerKey);
lockRequest.reject(new Error(`Lock could not be acquired within ${timeout}ms`));
}
// if we are rolling back, do nothing
else if(lockRequest.waitUntil && now < lockRequest.waitUntil /*|| document.readyState !== "complete"*/) {
// do nothing
//console.log(lockRequest.id, "skipping");
}
else if(lockRequest.state === "requested" ) {
let outerOwner = getItem(lockRequest.outerKey);
// if we aren't the outer owner anymore, lets try again
if (!outerOwner || (outerOwner !== lockRequest.id) ) {
// Write out the lockRequest's name on the outer key.
console.log(new Date().getTime(), lockRequest.id, "set outer. Outer was ", outerOwner);
setItem(lockRequest.outerKey, lockRequest.id);
lockRequest.state = "set-outer";
} else {
outerKeyAlreadyOwned(lockRequest, now, outerOwner);
}
} else if(lockRequest.state === "set-outer") {
// make sure we still own the outer state
let outerOwner = getItem(lockRequest.outerKey);
if(outerOwner !== lockRequest.id) {
// moves back to requested
outerKeyAlreadyOwned(lockRequest, now, outerOwner);
} else {
let innerOwner = getItem(lockRequest.innerKey);
if( innerOwner && innerOwner !== lockRequest.id ) {
// keeps at set-outer
innerKeyAlreadyOwned(lockRequest, now, innerOwner);
} else {
setItem(lockRequest.innerKey, lockRequest.id);
lockRequest.state = "set-inner";
console.log(new Date().getTime(), lockRequest.id, "set innerKey. InnerKey was", innerOwner );
}
}
} else if(lockRequest.state === "set-inner") {
// still make sure we own everything ...
let outerOwner = getItem(lockRequest.outerKey);
if(outerOwner !== lockRequest.id) {
// moves back to requested
outerKeyAlreadyOwned(lockRequest, now, outerOwner);
} else {
let innerOwner = getItem(lockRequest.innerKey);
if( innerOwner !== lockRequest.id ) {
// might still be null or another tab, set to set-outer, so we can keep trying
innerKeyAlreadyOwned(lockRequest, now, innerOwner);
} else {
lockLockRequest(lockRequest, now,
// fulfilled
(value) => {
console.log(new Date().getTime(), lockRequest.id, "unlocked");
onunlock(lockRequest.id);
// if already complete, do nothing
if(!COMPLETED_STATES[lockRequest.state]) {
lockRequest.state = "unlocked";
removeItem(lockRequest.innerKey);
removeItem(lockRequest.outerKey);
lockRequest.resolve();
}
},
// failure
(reason)=> {
console.log(new Date().getTime(), lockRequest.id, "unlocked-error");
onunlock()
if(!COMPLETED_STATES[lockRequest.state]) {
lockRequest.state = "unlocked";
removeItem(lockRequest.innerKey);
removeItem(lockRequest.outerKey);
lockRequest.reject(reason);
}
}
);
}
}
}
else if(COMPLETED_STATES[lockRequest.state] === true || lockRequest.state === "locked") {
// do nothing, the state will clear
}
else {
console.log(new Date().getTime(), lockRequest.id, "unexpected-state", progressLockState);
lockRequest.reject(new Error(`Unexpected state ${lockRequest.state}`));
lockRequest.state = "unexpected-state";
}
return lockRequest.state;
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue only identifies the progressLockState_double_lock(lockRequest, now) function and provides its implementation; start by reading that state machine and its callers in the repository. No target behavior, tests, or acceptance criteria are given, so the intended refactor and definition of done need to be established before work begins.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- web-dev
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 15/100