more robust storage path handling

This commit is contained in:
dragonwocky 2021-10-05 00:09:39 +11:00
parent 7b2761460e
commit f7a888e590

View File

@ -21,6 +21,7 @@ const _queue = [],
* @returns {Promise} value ?? fallback
*/
export const get = (path, fallback = undefined) => {
if (typeof path === 'string') path = [path];
if (!path.length) return fallback;
return new Promise((res, rej) =>
chrome.storage.local.get(async (values) => {
@ -44,6 +45,7 @@ export const get = (path, fallback = undefined) => {
* @returns {Promise} resolves when data has been saved
*/
export const set = (path, value) => {
if (typeof path === 'string') path = [path];
if (!path.length) return undefined;
const precursor = _queue[_queue.length - 1] || undefined,
interaction = new Promise(async (res, rej) => {
@ -86,6 +88,7 @@ export const set = (path, value) => {
* @returns {object} an object with the wrapped get/set functions
*/
export const db = (namespace, getFunc = get, setFunc = set) => {
if (typeof namespace === 'string') namespace = [namespace];
return {
get: (path = [], fallback = undefined) => getFunc([...namespace, ...path], fallback),
set: (path, value) => setFunc([...namespace, ...path], value),