Skip to content

Commit

Permalink
fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Mar 5, 2020
1 parent 7f90f87 commit 141ac83
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bundle/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,16 @@ var SuperTokensLock = /** @class */ (function () {
STORAGE = window.localStorage;
_a.label = 1;
case 1:
if (!(Date.now() < MAX_TIME)) return [3 /*break*/, 7];
if (!(Date.now() < MAX_TIME)) return [3 /*break*/, 8];
return [4 /*yield*/, delay(30)];
case 2:
_a.sent();
lockObj = STORAGE.getItem(STORAGE_KEY);
if (!(lockObj === null)) return [3 /*break*/, 4];
if (!(lockObj === null)) return [3 /*break*/, 5];
TIMEOUT_KEY = this.id + "-" + lockKey + "-" + iat;
// there is a problem if setItem happens at the exact same time for 2 different processes.. so we add some random delay here.
return [4 /*yield*/, delay(Math.floor(Math.random() * 25))];
case 2:
case 3:
// there is a problem if setItem happens at the exact same time for 2 different processes.. so we add some random delay here.
_a.sent();
STORAGE.setItem(STORAGE_KEY, JSON.stringify({
Expand All @@ -137,7 +140,7 @@ var SuperTokensLock = /** @class */ (function () {
timeRefreshed: Date.now()
}));
return [4 /*yield*/, delay(30)];
case 3:
case 4:
_a.sent(); // this is to prevent race conditions. This time must be more than the time it takes for storage.setItem
lockObjPostDelay = STORAGE.getItem(STORAGE_KEY);
if (lockObjPostDelay !== null) {
Expand All @@ -148,17 +151,17 @@ var SuperTokensLock = /** @class */ (function () {
return [2 /*return*/, true];
}
}
return [3 /*break*/, 6];
case 4:
return [3 /*break*/, 7];
case 5:
SuperTokensLock.lockCorrector();
return [4 /*yield*/, this.waitForSomethingToChange(MAX_TIME)];
case 5:
_a.sent();
_a.label = 6;
case 6:
_a.sent();
_a.label = 7;
case 7:
iat = Date.now() + generateRandomString(4);
return [3 /*break*/, 1];
case 7: return [2 /*return*/, false];
case 8: return [2 /*return*/, false];
}
});
});
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default class SuperTokensLock {
const STORAGE_KEY = `${LOCK_STORAGE_KEY}-${lockKey}`;
const STORAGE = window.localStorage;
while (Date.now() < MAX_TIME) {
await delay(30);
let lockObj = STORAGE.getItem(STORAGE_KEY);
if (lockObj === null) {
const TIMEOUT_KEY = `${this.id}-${lockKey}-${iat}`;
Expand Down
Loading

0 comments on commit 141ac83

Please sign in to comment.