Skip to content

Commit

Permalink
Merge pull request #3816 from G-Ambatte/refactorVersionHistory
Browse files Browse the repository at this point in the history
Refactor version history to use custom store wrapper
  • Loading branch information
calculuschild authored Oct 29, 2024
2 parents 8a8bf88 + ea25ae6 commit c7c8daf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
19 changes: 19 additions & 0 deletions client/homebrew/utils/customIDBStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as IDB from 'idb-keyval/dist/index.js';

export function initCustomStore(db, store){
const createCustomStore = async ()=>IDB.createStore(db, store);

return {
entries : async ()=>IDB.entries(await createCustomStore()),
keys : async ()=>IDB.keys(await createCustomStore()),
values : async ()=>IDB.values(await createCustomStore()),
clear : async ()=>IDB.clear(await createCustomStore),
get : async (key)=>IDB.get(key, await createCustomStore()),
getMany : async (keys)=>IDB.getMany(keys, await createCustomStore()),
set : async (key, value)=>IDB.set(key, value, await createCustomStore()),
setMany : async (entries)=>IDB.setMany(entries, await createCustomStore()),
update : async (key, updateFn)=>IDB.update(key, updateFn, await createCustomStore()),
del : async (key)=>IDB.del(key, await createCustomStore()),
delMany : async (keys)=>IDB.delMany(keys, await createCustomStore())
};
};
28 changes: 14 additions & 14 deletions client/homebrew/utils/versionHistory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as IDB from 'idb-keyval/dist/index.js';
import { initCustomStore } from './customIDBStore.js';

export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY';
export const HISTORY_SLOTS = 5;
Expand All @@ -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}`;
Expand All @@ -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 };

Expand All @@ -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; });
}

Expand Down Expand Up @@ -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;
Expand All @@ -106,14 +103,17 @@ export async function updateHistory(brew) {
};

export async function versionHistoryGarbageCollection(){
const entries = await IDB.entries();

const entries = await IDB.entries(await createHBStore());

const expiredKeys = [];
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());
expiredKeys.push(key);
};
};
if(expiredKeys.length > 0){
await IDB.delMany(expiredKeys);
}
};

0 comments on commit c7c8daf

Please sign in to comment.