Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor config storage to use modules
- Loading branch information
1 parent
b287361
commit c1d3fee
Showing
15 changed files
with
149 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,71 @@ | ||
'use strict'; | ||
/** | ||
* Gets one or more items from localStorage | ||
* @param {string | string[]} key string or array of strings | ||
* @returns {object} | ||
*/ | ||
export function get(key) { | ||
let result = {}; | ||
if (Array.isArray(key)) { | ||
key.forEach(function (element) { | ||
try { | ||
result = { ...result, ...JSON.parse(localStorage.getItem(element)) }; | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
} else { | ||
const keyValue = localStorage.getItem(key); | ||
if (keyValue) { | ||
try { | ||
result = JSON.parse(keyValue); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
|
||
// idea here is to abstract around the use of chrome.storage.local as it functions differently from "localStorage" and IndexedDB | ||
// localStorage deals with strings, not objects, so the objects have been serialized. | ||
const ConfigStorage = { | ||
// key can be one string, or array of strings | ||
get: function(key) { | ||
let result = {}; | ||
if (Array.isArray(key)) { | ||
key.forEach(function (element) { | ||
try { | ||
result = {...result, ...JSON.parse(localStorage.getItem(element))}; | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
} else { | ||
const keyValue = localStorage.getItem(key); | ||
if (keyValue) { | ||
try { | ||
result = JSON.parse(keyValue); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// set takes an object like {'userLanguageSelect':'DEFAULT'} | ||
/** | ||
* Save dictionary of key/value pairs to localStorage | ||
* @param {object} input object which keys are strings and values are serializable objects | ||
*/ | ||
export function set(input) { | ||
Object.keys(input).forEach(function (element) { | ||
const tmpObj = {}; | ||
tmpObj[element] = input[element]; | ||
try { | ||
localStorage.setItem(element, JSON.stringify(tmpObj)); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Remove item from localStorage | ||
* @param {string} item key to remove from storage | ||
*/ | ||
export function remove(item) { | ||
localStorage.removeItem(item); | ||
} | ||
|
||
return result; | ||
}, | ||
// set takes an object like {'userLanguageSelect':'DEFAULT'} | ||
set: function(input) { | ||
Object.keys(input).forEach(function (element) { | ||
const tmpObj = {}; | ||
tmpObj[element] = input[element]; | ||
try { | ||
localStorage.setItem(element, JSON.stringify(tmpObj)); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
}, | ||
remove: function(item) { | ||
localStorage.removeItem(item); | ||
}, | ||
clear: function() { | ||
localStorage.clear(); | ||
}, | ||
/** | ||
* Clear localStorage | ||
*/ | ||
export function clear() { | ||
localStorage.clear(); | ||
} | ||
|
||
/** | ||
* @deprecated this is a temporary solution to allow the use of the ConfigStorage module in old way | ||
*/ | ||
const ConfigStorage = { | ||
get, | ||
set, | ||
remove, | ||
clear, | ||
}; | ||
window.ConfigStorage = ConfigStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.