diff --git a/client/homebrew/utils/customIDBStoreWrapper.js b/client/homebrew/utils/customIDBStoreWrapper.js new file mode 100644 index 000000000..c82ff858a --- /dev/null +++ b/client/homebrew/utils/customIDBStoreWrapper.js @@ -0,0 +1,54 @@ +import * as IDB from 'idb-keyval/dist/index.js'; + +export function initCustomStore(db, store){ + const createCustomStore = async ()=>{ + return await IDB.createStore(db, store); + }; + return { + entries : async ()=>{ + // Return all entries : [[key1, value1], [key2, value2], ... [keyN, valueN] ] + return await IDB.entries(await createCustomStore()); + }, + keys : async ()=>{ + // Return all keys : [ key1, key2, ... keyN ] + return await IDB.keys(await createCustomStore()); + }, + values : async ()=>{ + // Return all values : [ value1, value2, ... valueN ] + return await IDB.values(await createCustomStore()); + }, + clear : async ()=>{ + // Delete all keys and values + return await IDB.clear(await createCustomStore); + }, + get : async (key)=>{ + // Get a value by its key + return await IDB.get(key, await createCustomStore()); + }, + getMany : async (keys)=>{ + // Get multiple values at once + return await IDB.getMany(keys, await createCustomStore()); + }, + set : async (key, value)=>{ + // Set a value in the store by the key + return await IDB.set(key, value, await createCustomStore()); + }, + setMany : async (entries)=>{ + // Set multiple values at once + // `entries` is in the form : [ [key1, value1], [key2, value2], ... [keyN, valueN] ] + return await IDB.setMany(entries, await createCustomStore()); + }, + update : async (key, updateFn)=>{ + // Update a value in a single atomic action + return await IDB.update(key, updateFn, await createCustomStore()); + }, + del : async (key)=>{ + // Delete a single key and associated value from the store + return await IDB.del(key, await createCustomStore()); + }, + delMany : async (keys)=>{ + // Delete multiple keys at once + return await IDB.delMany(keys, await createCustomStore()); + } + }; +}; \ No newline at end of file diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index a23af844a..23f9e03bc 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -1,4 +1,4 @@ -import * as IDB from 'idb-keyval/dist/index.js'; +import { initCustomStore } from './customIDBStoreWrapper'; export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'; export const HISTORY_SLOTS = 5; @@ -21,13 +21,15 @@ const HISTORY_SAVE_DELAYS = { // '5' : 5 // }; -const HB_DB = 'HOMEBREWERY-DB'; -const HB_STORE = 'HISTORY'; - const GARBAGE_COLLECT_DELAY = 28 * 24 * 60; // const GARBAGE_COLLECT_DELAY = 10; +const HB_DB = 'HOMEBREWERY-DB'; +const HB_STORE = 'HISTORY'; + +const IDB = initCustomStore(HB_DB, HB_STORE); + function getKeyBySlot(brew, slot){ // Return a string representing the key for this brew and history slot return `${HISTORY_PREFIX}-${brew.shareId}-${slot}`; @@ -53,11 +55,6 @@ function parseBrewForStorage(brew, slot = 0) { return [key, archiveBrew]; } -// Create a custom IDB store -async function createHBStore(){ - return await IDB.createStore(HB_DB, HB_STORE); -} - export async function loadHistory(brew){ const DEFAULT_HISTORY_ITEM = { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; @@ -69,7 +66,7 @@ export async function loadHistory(brew){ }; // Load all keys from IDB at once - const dataArray = await IDB.getMany(historyKeys, await createHBStore()); + const dataArray = await IDB.getMany(historyKeys); return dataArray.map((data)=>{ return data ?? DEFAULT_HISTORY_ITEM; }); } @@ -97,7 +94,7 @@ export async function updateHistory(brew) { // Update the most recent brew historyUpdate.push(parseBrewForStorage(brew, 1)); - await IDB.setMany(historyUpdate, await createHBStore()); + await IDB.setMany(historyUpdate); // Break out of data checks because we found an expired value break; @@ -106,14 +103,13 @@ export async function updateHistory(brew) { }; export async function versionHistoryGarbageCollection(){ - - const entries = await IDB.entries(await createHBStore()); + const entries = await IDB.entries(); for (const [key, value] of entries){ const expireAt = new Date(value.savedAt); expireAt.setMinutes(expireAt.getMinutes() + GARBAGE_COLLECT_DELAY); if(new Date() > expireAt){ - await IDB.del(key, await createHBStore()); + await IDB.del(key); }; }; }; \ No newline at end of file