Skip to content

Commit

Permalink
Merge pull request #6 from supertokens/pre-release-1.2.3
Browse files Browse the repository at this point in the history
Pre release 1.2.3
  • Loading branch information
nkshah2 committed Nov 26, 2019
2 parents d60dbde + b597559 commit bd67106
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 88 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.

3 changes: 3 additions & 0 deletions bundleEntry.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import SuperTokensLock from "./index";
declare var getNewInstance: () => SuperTokensLock;
export { getNewInstance };
49 changes: 45 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
export default class Lock {
acquireLock: (lockKey: string, timeout?: number) => Promise<boolean>
releaseLock: (lockKey: string) => void
}
export default class SuperTokensLock {
private static waiters;
private id;
private acquiredIatSet;
constructor();
/**
* @async
* @memberOf Lock
* @function acquireLock
* @param {string} lockKey - Key for which the lock is being acquired
* @param {number} [timeout=5000] - Maximum time for which the function will wait to acquire the lock
* @returns {Promise<boolean>}
* @description Will return true if lock is being acquired, else false.
* Also the lock can be acquired for maximum 10 secs
*/
acquireLock(lockKey: string, timeout?: number): Promise<boolean>;
private refreshLockWhileAcquired;
private waitForSomethingToChange;
private static addToWaiting;
private static removeFromWaiting;
private static notifyWaiters;
/**
* @function releaseLock
* @memberOf Lock
* @param {string} lockKey - Key for which lock is being released
* @returns {void}
* @description Release a lock.
*/
releaseLock(lockKey: string): Promise<void>;
/**
* @function releaseLock
* @memberOf Lock
* @param {string} lockKey - Key for which lock is being released
* @returns {void}
* @description Release a lock.
*/
private releaseLock__private__;
/**
* @function lockCorrector
* @returns {void}
* @description If a lock is acquired by a tab and the tab is closed before the lock is
* released, this function will release those locks
*/
private static lockCorrector;
}
61 changes: 30 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ function generateRandomString(length) {
var INDEX = Math.floor(Math.random() * CHARS.length);
randomstring += CHARS[INDEX];
}
//TODO: add supertokens to the random string, total length will be 21
return randomstring;
}
/**
Expand Down Expand Up @@ -151,7 +150,7 @@ var SuperTokensLock = /** @class */ (function () {
}
return [3 /*break*/, 6];
case 4:
lockCorrector();
SuperTokensLock.lockCorrector();
return [4 /*yield*/, this.waitForSomethingToChange(MAX_TIME)];
case 5:
_a.sent();
Expand Down Expand Up @@ -309,36 +308,36 @@ var SuperTokensLock = /** @class */ (function () {
});
});
};
/**
* @function lockCorrector
* @returns {void}
* @description If a lock is acquired by a tab and the tab is closed before the lock is
* released, this function will release those locks
*/
SuperTokensLock.lockCorrector = function () {
var MIN_ALLOWED_TIME = Date.now() - 5000;
var STORAGE = window.localStorage;
var KEYS = Object.keys(STORAGE);
var notifyWaiters = false;
for (var i = 0; i < KEYS.length; i++) {
var LOCK_KEY = KEYS[i];
if (LOCK_KEY.includes(LOCK_STORAGE_KEY)) {
var lockObj = STORAGE.getItem(LOCK_KEY);
if (lockObj !== null) {
lockObj = JSON.parse(lockObj);
if ((lockObj.timeRefreshed === undefined && lockObj.timeAcquired < MIN_ALLOWED_TIME) ||
(lockObj.timeRefreshed !== undefined && lockObj.timeRefreshed < MIN_ALLOWED_TIME)) {
STORAGE.removeItem(LOCK_KEY);
notifyWaiters = true;
}
}
}
}
if (notifyWaiters) {
SuperTokensLock.notifyWaiters();
}
};
SuperTokensLock.waiters = undefined;
return SuperTokensLock;
}());
exports.default = SuperTokensLock;
/**
* @function lockCorrector
* @returns {void}
* @description If a lock is acquired by a tab and the tab is closed before the lock is
* released, this function will release those locks
*/
function lockCorrector() {
var MIN_ALLOWED_TIME = Date.now() - 5000;
var STORAGE = window.localStorage;
var KEYS = Object.keys(STORAGE);
var notifyWaiters = false;
for (var i = 0; i < KEYS.length; i++) {
var LOCK_KEY = KEYS[i];
if (LOCK_KEY.includes(LOCK_STORAGE_KEY)) {
var lockObj = STORAGE.getItem(LOCK_KEY);
if (lockObj !== null) {
lockObj = JSON.parse(lockObj);
if ((lockObj.timeRefreshed === undefined && lockObj.timeAcquired < MIN_ALLOWED_TIME) ||
(lockObj.timeRefreshed !== undefined && lockObj.timeRefreshed < MIN_ALLOWED_TIME)) {
STORAGE.removeItem(LOCK_KEY);
notifyWaiters = true;
}
}
}
}
if (notifyWaiters) {
SuperTokensLock.notifyWaiters();
}
}
Loading

0 comments on commit bd67106

Please sign in to comment.