Skip to content

Commit

Permalink
refactor storageManager/sessionStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed Jul 8, 2024
1 parent 2ebd71b commit 4c6e7a0
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 255 deletions.
209 changes: 56 additions & 153 deletions src/storageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,6 @@ export function newStorageManager({moduleName, moduleType} = {}, {isAllowed = is
return schedule(cb, STORAGE_TYPE_COOKIES, done);
};

/**
* @returns {boolean}
*/
const localStorageIsEnabled = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
localStorage.setItem('prebid.cookieTest', '1');
return localStorage.getItem('prebid.cookieTest') === '1';
} catch (error) {
} finally {
try {
localStorage.removeItem('prebid.cookieTest');
} catch (error) {}
}
}
return false;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @returns {boolean}
*/
Expand All @@ -122,137 +101,69 @@ export function newStorageManager({moduleName, moduleType} = {}, {isAllowed = is
return schedule(cb, STORAGE_TYPE_COOKIES, done);
}

/**
* @param {string} key
* @param {string} value
*/
const setDataInLocalStorage = function (key, value, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
window.localStorage.setItem(key, value);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @param {string} key
* @returns {(string|null)}
*/
const getDataFromLocalStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
return window.localStorage.getItem(key);
}
return null;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @param {string} key
*/
const removeDataFromLocalStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
window.localStorage.removeItem(key);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @returns {boolean}
*/
const hasLocalStorage = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
return !!window.localStorage;
} catch (e) {
logError('Local storage api disabled');
}
}
return false;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
function storageMethods(name) {
const capName = name.charAt(0).toUpperCase() + name.substring(1);
const backend = () => window[name];

/**
* @returns {boolean}
*/
const sessionStorageIsEnabled = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
sessionStorage.setItem('prebid.cookieTest', '1');
return sessionStorage.getItem('prebid.cookieTest') === '1';
} catch (error) {
} finally {
const hasStorage = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
sessionStorage.removeItem('prebid.cookieTest');
} catch (error) {}
return !!backend();
} catch (e) {
logError(`${name} api disabled`);
}
}
return false;
}
return false;
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @param {string} key
* @param {string} value
*/
const setDataInSessionStorage = function (key, value, done) {
let cb = function (result) {
if (result && result.valid && hasSessionStorage()) {
window.sessionStorage.setItem(key, value);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @param {string} key
* @returns {(string|null)}
*/
const getDataFromSessionStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasSessionStorage()) {
return window.sessionStorage.getItem(key);
}
return null;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @param {string} key
*/
const removeDataFromSessionStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasSessionStorage()) {
window.sessionStorage.removeItem(key);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
* @returns {boolean}
*/
const hasSessionStorage = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
return !!window.sessionStorage;
} catch (e) {
logError('Session storage api disabled');
return {
[`has${capName}`]: hasStorage,
[`${name}IsEnabled`](done) {
let cb = function (result) {
if (result && result.valid) {
try {
backend().setItem('prebid.cookieTest', '1');
return backend().getItem('prebid.cookieTest') === '1';
} catch (error) {
} finally {
try {
backend().removeItem('prebid.cookieTest');
} catch (error) {}
}
}
return false;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
},
[`setDataIn${capName}`](key, value, done) {
let cb = function (result) {
if (result && result.valid && hasStorage()) {
backend().setItem(key, value);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
},
[`getDataFrom${capName}`](key, done) {
let cb = function (result) {
if (result && result.valid && hasStorage()) {
return backend().getItem(key);
}
return null;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
},
[`removeDataFrom${capName}`](key, done) {
let cb = function (result) {
if (result && result.valid && hasStorage()) {
backend().removeItem(key);
}
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}
return false;
}
return schedule(cb, STORAGE_TYPE_LOCALSTORAGE, done);
}

/**
Expand Down Expand Up @@ -288,17 +199,9 @@ export function newStorageManager({moduleName, moduleType} = {}, {isAllowed = is
return {
setCookie,
getCookie,
localStorageIsEnabled,
cookiesAreEnabled,
setDataInLocalStorage,
getDataFromLocalStorage,
removeDataFromLocalStorage,
hasLocalStorage,
sessionStorageIsEnabled,
setDataInSessionStorage,
getDataFromSessionStorage,
removeDataFromSessionStorage,
hasSessionStorage,
...storageMethods('localStorage'),
...storageMethods('sessionStorage'),
findSimilarCookies
}
}
Expand Down
Loading

0 comments on commit 4c6e7a0

Please sign in to comment.