From 77f162f7a4e556b183b5c276f5808617ce1639f8 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 1 Oct 2024 23:53:35 +1300 Subject: [PATCH 01/21] Initial IDB functionality pass --- .../homebrew/editor/snippetbar/snippetbar.jsx | 4 +- client/homebrew/utils/useIDB.js | 80 ++++++++++++++++ client/homebrew/utils/versionHistory.js | 94 +++++++++++++------ package-lock.json | 6 ++ package.json | 3 +- 5 files changed, 155 insertions(+), 32 deletions(-) create mode 100644 client/homebrew/utils/useIDB.js diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index f183e0876..12658cd41 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -70,10 +70,10 @@ const Snippetbar = createClass({ if(historyExists(this.props.brew) != this.state.historyExists){ this.setState({ - historyExists : !this.state.historyExists + historyExists : !this.state.historyExists }); }; - + }, mergeCustomizer : function(oldValue, newValue, key) { diff --git a/client/homebrew/utils/useIDB.js b/client/homebrew/utils/useIDB.js new file mode 100644 index 000000000..03404160a --- /dev/null +++ b/client/homebrew/utils/useIDB.js @@ -0,0 +1,80 @@ +import { + get as iGet, + getMany as iGetMany, + set as iSet, + setMany as iSetMany, + update as iUpdate, + del as iDel, + keys, + createStore, + Store +} from 'idb-keyval/dist/index.js'; // EcmaScript Module + +const HOMEBREWERY_DB = 'HOMEBREWERY-DB'; +const HOMEBREWERY_STORE = 'HOMEBREWERY-STORE'; + +let hbStore; + +function init(){ + if(hbStore) return true; + if(!hbStore && typeof window != 'undefined' && typeof window.indexedDB != 'undefined'){ + hbStore = createStore(HOMEBREWERY_DB, HOMEBREWERY_STORE); + return true; + } + return false; +} + +function checkFn(fn){ + return init() && fn(); +}; + +const get = checkFn(async (key)=>{ + console.log('get:', key); + return iGet(key, hbStore); +}); + +const getMany = checkFn(async (keys)=>{ + checkFn(async ()=>{ + console.log('getMany:', keys); + return await iGetMany(keys, hbStore); + }); +}); + +const set = checkFn(async (key, val)=>{ + console.log('set:', key, val); + init(); + return iSet(key, val, hbStore); +}); + +const setMany = checkFn(async (keyValArray)=>{ + console.log('set:', keyValArray); + init(); + return iSetMany(keyValArray, hbStore); +}); + + +const update = checkFn(async (key, updateFn)=>{ + init(); + return iUpdate(key, updateFn, hbStore); +}); + +const remove = checkFn(async (key)=>{ + console.log('remove:', key); + init(); + return iDel(key, hbStore); +}); + +const list = checkFn(async ()=>{ + init(); + return await keys(hbStore); +}); + +module.exports = { + get, + getMany, + set, + setMany, + update, + remove, + list +}; \ No newline at end of file diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index ad7c6102e..b86dc66e0 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -1,3 +1,5 @@ +import * as IDB from 'idb-keyval/dist/index.js'; + export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'; export const HISTORY_SLOTS = 5; @@ -27,12 +29,12 @@ function getVersionBySlot(brew, slot){ // - If it exists, parse data to object // - If it doesn't exist, pass default object const key = getKeyBySlot(brew, slot); - const storedVersion = localStorage.getItem(key); - const output = storedVersion ? JSON.parse(storedVersion) : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; + const storedVersion = IDB.get(key); + const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; return output; }; -function updateStoredBrew(brew, slot = 0) { +function parseBrewForStorage(brew, slot = 0) { const archiveBrew = { title : brew.title, text : brew.text, @@ -46,44 +48,77 @@ function updateStoredBrew(brew, slot = 0) { archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]); const key = getKeyBySlot(brew, slot); - localStorage.setItem(key, JSON.stringify(archiveBrew)); + + return [key, archiveBrew]; } -export function historyExists(brew){ - return Object.keys(localStorage) - .some((key)=>{ - return key.startsWith(`${HISTORY_PREFIX}-${brew.shareId}`); - }); +export async function historyExists(brew){ + console.log('HISTORY CHECK'); + const historyExists = await IDB.keys() + .then((keys)=>{ + return [...keys].some((key)=>{ + return key.startsWith(`${HISTORY_PREFIX}-${brew.shareId}`); + }); + }) + .catch(()=>{return false;}); + console.log('HISTORY STATE:', historyExists); } -export function loadHistory(brew){ - const history = {}; +export async function loadHistory(brew){ + const DEFAULT_HISTORY_ITEM = { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; + + const historyKeys = []; // Load data from local storage to History object for (let i = 1; i <= HISTORY_SLOTS; i++){ - history[i] = getVersionBySlot(brew, i); + historyKeys.push(getKeyBySlot(brew, i)); }; + const history = []; + await IDB.getMany(historyKeys) + .then((dataArray)=>{ + return dataArray.forEach((data)=>{ + history.push(data ?? DEFAULT_HISTORY_ITEM); + }); + }) + .catch(()=>{ + historyKeys.forEach(()=>{ + history.push(DEFAULT_HISTORY_ITEM); + }); + }); + return history; } -export function updateHistory(brew) { - const history = loadHistory(brew); +export async function updateHistory(brew) { + const history = await loadHistory(brew); + + console.log('DATA:', history); // Walk each version position - for (let slot = HISTORY_SLOTS; slot > 0; slot--){ + for (let slot = HISTORY_SLOTS - 1; slot >= 0; slot--){ + console.log('SLOT #:', slot, history[slot]); const storedVersion = history[slot]; // If slot has expired, update all lower slots and break if(new Date() >= new Date(storedVersion.expireAt)){ - for (let updateSlot = slot - 1; updateSlot>0; updateSlot--){ + const historyUpdate = []; + + for (let updateSlot = slot - 1; updateSlot > 0; updateSlot--){ + console.log('CHECK DATA IN SLOT #:', updateSlot); // Move data from updateSlot to updateSlot + 1 - !history[updateSlot]?.noData && updateStoredBrew(history[updateSlot], updateSlot + 1); + if(!history[updateSlot - 1]?.noData) { + console.log('UPDATE SLOT #:', updateSlot - 1, '>', updateSlot); + historyUpdate.push(parseBrewForStorage(history[updateSlot - 1], updateSlot + 1)); + } }; // Update the most recent brew - updateStoredBrew(brew, 1); + historyUpdate.push(parseBrewForStorage(brew, 1)); + + console.log('HISTORY UPDATE OBJECT:', historyUpdate); + IDB.setMany(historyUpdate); // Break out of data checks because we found an expired value break; @@ -102,15 +137,16 @@ export function getHistoryItems(brew){ }; export function versionHistoryGarbageCollection(){ - Object.keys(localStorage) - .filter((key)=>{ - return key.startsWith(HISTORY_PREFIX); - }) - .forEach((key)=>{ - const collectAt = new Date(JSON.parse(localStorage.getItem(key)).savedAt); - collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); - if(new Date() > collectAt){ - localStorage.removeItem(key); - } - }); + console.log('Version History Garbage Collection'); + // Object.keys(IDB) + // .filter((key)=>{ + // return key.startsWith(HISTORY_PREFIX); + // }) + // .forEach((key)=>{ + // const collectAt = new Date(JSON.parse(IDB.get(key)).savedAt); + // collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); + // if(new Date() > collectAt){ + // IDB.removeItem(key); + // } + // }); }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index cb0299d4e..9645f64e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "express-async-handler": "^1.2.0", "express-static-gzip": "2.1.8", "fs-extra": "11.2.0", + "idb-keyval": "^6.2.1", "js-yaml": "^4.1.0", "jwt-simple": "^0.5.6", "less": "^3.13.1", @@ -7451,6 +7452,11 @@ "node": ">=0.10.0" } }, + "node_modules/idb-keyval": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/idb-keyval/-/idb-keyval-6.2.1.tgz", + "integrity": "sha512-8Sb3veuYCyrZL+VBt9LJfZjLUPWVvqn8tG28VqYNFCo43KHcKuq+b4EiXGeuaLAQWL2YmyDgMp2aSpH9JHsEQg==" + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", diff --git a/package.json b/package.json index c78a08f25..917a09a96 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "express-async-handler": "^1.2.0", "express-static-gzip": "2.1.8", "fs-extra": "11.2.0", + "idb-keyval": "^6.2.1", "js-yaml": "^4.1.0", "jwt-simple": "^0.5.6", "less": "^3.13.1", @@ -138,4 +139,4 @@ "stylelint-config-recommended": "^14.0.1", "supertest": "^7.0.0" } -} \ No newline at end of file +} From c9241e30914d2ab0e8a4c1222ba9223a54006752 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 18:22:06 +1300 Subject: [PATCH 02/21] WIP Update --- .../homebrew/editor/snippetbar/snippetbar.jsx | 61 ++++++++++--------- client/homebrew/utils/versionHistory.js | 52 +++++++--------- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 12658cd41..3a7b48916 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -5,7 +5,7 @@ const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); -import { getHistoryItems, historyExists } from '../../utils/versionHistory.js'; +import { historyCheck, loadHistory } from '../../utils/versionHistory.js'; //Import all themes const ThemeSnippets = {}; @@ -50,30 +50,31 @@ const Snippetbar = createClass({ renderer : this.props.renderer, themeSelector : false, snippets : [], - historyExists : false + historyExists : false, + showHistory : false }; }, - componentDidMount : async function() { + componentDidMount : async function(prevState) { const snippets = this.compileSnippets(); this.setState({ snippets : snippets }); }, - componentDidUpdate : async function(prevProps) { + componentDidUpdate : async function(prevProps, prevState) { if(prevProps.renderer != this.props.renderer || prevProps.theme != this.props.theme || prevProps.snippetBundle != this.props.snippetBundle) { this.setState({ snippets : this.compileSnippets() }); }; - if(historyExists(this.props.brew) != this.state.historyExists){ + const checkHistoryExists = (await historyCheck(this.props.brew)) ?? false; + if(checkHistoryExists != prevState.historyExists){ this.setState({ - historyExists : !this.state.historyExists + historyExists : checkHistoryExists }); - }; - + } }, mergeCustomizer : function(oldValue, newValue, key) { @@ -151,30 +152,34 @@ const Snippetbar = createClass({ return this.props.updateBrew(item); }, - renderHistoryItems : function() { - const historyItems = getHistoryItems(this.props.brew); + renderHistoryItems : async function() { + if(!this.state.historyExists || !this.state.showHistory) return; - return
- {_.map(historyItems, (item, index)=>{ - if(!item.savedAt) return; + const historyItems = await loadHistory(this.props.brew); + console.log('renderHistoryItems', historyItems); + return; - const saveTime = new Date(item.savedAt); - const diffMs = new Date() - saveTime; - const diffSecs = Math.floor(diffMs / 1000); + // return
+ // {_.map(historyItems, (item, index)=>{ + // if(!item[1].savedAt) return; - let diffString = `about ${diffSecs} seconds ago`; + // const saveTime = new Date(item[1].savedAt); + // const diffMs = new Date() - saveTime; + // const diffSecs = Math.floor(diffMs / 1000); - if(diffSecs > 60) diffString = `about ${Math.floor(diffSecs / 60)} minutes ago`; - if(diffSecs > (60 * 60)) diffString = `about ${Math.floor(diffSecs / (60 * 60))} hours ago`; - if(diffSecs > (24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (24 * 60 * 60))} days ago`; - if(diffSecs > (7 * 24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (7 * 24 * 60 * 60))} weeks ago`; + // let diffString = `about ${diffSecs} seconds ago`; - return
{this.replaceContent(item);}} > - - v{item.version} : {diffString} -
; - })} -
; + // if(diffSecs > 60) diffString = `about ${Math.floor(diffSecs / 60)} minutes ago`; + // if(diffSecs > (60 * 60)) diffString = `about ${Math.floor(diffSecs / (60 * 60))} hours ago`; + // if(diffSecs > (24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (24 * 60 * 60))} days ago`; + // if(diffSecs > (7 * 24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (7 * 24 * 60 * 60))} weeks ago`; + + // return
{this.replaceContent(item);}} > + // + // v{item.version} : {diffString} + //
; + // })} + //
; }, renderEditorButtons : function(){ @@ -199,7 +204,7 @@ const Snippetbar = createClass({ return
- {this.state.historyExists && this.renderHistoryItems() } + {/* { this.renderHistoryItems() } */}
diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index b86dc66e0..b72ab6db6 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -21,20 +21,23 @@ const GARBAGE_COLLECT_DELAY = global.config?.historyData?.GARBAGE_COLLECT_DELAY function getKeyBySlot(brew, slot){ + // Return a string representing the key for this brew and history slot return `${HISTORY_PREFIX}-${brew.shareId}-${slot}`; }; -function getVersionBySlot(brew, slot){ - // Read stored brew data - // - If it exists, parse data to object - // - If it doesn't exist, pass default object - const key = getKeyBySlot(brew, slot); - const storedVersion = IDB.get(key); - const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; - return output; -}; +// function getVersionBySlot(brew, slot){ +// // Read stored brew data +// // - If it exists, parse data to object +// // - If it doesn't exist, pass default object +// const key = getKeyBySlot(brew, slot); +// const storedVersion = IDB.get(key); +// const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; +// return output; +// }; function parseBrewForStorage(brew, slot = 0) { + // Strip out unneeded object properties + // Returns an array of [ key, brew ] const archiveBrew = { title : brew.title, text : brew.text, @@ -53,8 +56,8 @@ function parseBrewForStorage(brew, slot = 0) { } -export async function historyExists(brew){ - console.log('HISTORY CHECK'); +export async function historyCheck(brew){ + if(!IDB) return false; const historyExists = await IDB.keys() .then((keys)=>{ return [...keys].some((key)=>{ @@ -62,7 +65,8 @@ export async function historyExists(brew){ }); }) .catch(()=>{return false;}); - console.log('HISTORY STATE:', historyExists); + + return historyExists; } export async function loadHistory(brew){ @@ -70,12 +74,13 @@ export async function loadHistory(brew){ const historyKeys = []; - // Load data from local storage to History object + // Create array of all history keys for (let i = 1; i <= HISTORY_SLOTS; i++){ historyKeys.push(getKeyBySlot(brew, i)); }; const history = []; + // Load all keys from IDB at once await IDB.getMany(historyKeys) .then((dataArray)=>{ return dataArray.forEach((data)=>{ @@ -94,22 +99,20 @@ export async function loadHistory(brew){ export async function updateHistory(brew) { const history = await loadHistory(brew); - console.log('DATA:', history); - // Walk each version position for (let slot = HISTORY_SLOTS - 1; slot >= 0; slot--){ - console.log('SLOT #:', slot, history[slot]); const storedVersion = history[slot]; // If slot has expired, update all lower slots and break if(new Date() >= new Date(storedVersion.expireAt)){ + + // Create array of arrays : [ [key1, value1], [key2, value2], ..., [keyN, valueN] ] + // to pass to IDB.setMany const historyUpdate = []; - for (let updateSlot = slot - 1; updateSlot > 0; updateSlot--){ - console.log('CHECK DATA IN SLOT #:', updateSlot); + for (let updateSlot = slot; updateSlot > 0; updateSlot--){ // Move data from updateSlot to updateSlot + 1 if(!history[updateSlot - 1]?.noData) { - console.log('UPDATE SLOT #:', updateSlot - 1, '>', updateSlot); historyUpdate.push(parseBrewForStorage(history[updateSlot - 1], updateSlot + 1)); } }; @@ -117,7 +120,6 @@ export async function updateHistory(brew) { // Update the most recent brew historyUpdate.push(parseBrewForStorage(brew, 1)); - console.log('HISTORY UPDATE OBJECT:', historyUpdate); IDB.setMany(historyUpdate); // Break out of data checks because we found an expired value @@ -126,16 +128,6 @@ export async function updateHistory(brew) { }; }; -export function getHistoryItems(brew){ - const historyArray = []; - - for (let i = 1; i <= HISTORY_SLOTS; i++){ - historyArray.push(getVersionBySlot(brew, i)); - } - - return historyArray; -}; - export function versionHistoryGarbageCollection(){ console.log('Version History Garbage Collection'); // Object.keys(IDB) From 25ce1aa00cde4bc6096937aeded17a9a60167da7 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 19:36:17 +1300 Subject: [PATCH 03/21] Functional history menu --- .../homebrew/editor/snippetbar/snippetbar.jsx | 65 ++++++++++++------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 3a7b48916..29ce4d8c8 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -50,8 +50,9 @@ const Snippetbar = createClass({ renderer : this.props.renderer, themeSelector : false, snippets : [], + showHistory : false, historyExists : false, - showHistory : false + historyItems : [] }; }, @@ -75,6 +76,17 @@ const Snippetbar = createClass({ historyExists : checkHistoryExists }); } + + // Update history list if it has changed + const checkHistoryItems = await loadHistory(this.props.brew); + if(checkHistoryItems.some((historyItem, index)=>{ + return !index < this.state.historyItems.length || historyItem != this.state.historyItems[index]; + })){ + this.setState({ + historyItems : checkHistoryItems + }); + } + }, mergeCustomizer : function(oldValue, newValue, key) { @@ -152,34 +164,36 @@ const Snippetbar = createClass({ return this.props.updateBrew(item); }, - renderHistoryItems : async function() { - if(!this.state.historyExists || !this.state.showHistory) return; + toggleHistoryMenu : function(){ + this.setState({ + showHistory : !this.state.showHistory + }); + }, - const historyItems = await loadHistory(this.props.brew); - console.log('renderHistoryItems', historyItems); - return; + renderHistoryItems : function() { + if(this.state.historyItems.length == 0) return; - // return
- // {_.map(historyItems, (item, index)=>{ - // if(!item[1].savedAt) return; + return
+ {_.map(this.state.historyItems, (item, index)=>{ + if(item.noData || !item.savedAt) return; - // const saveTime = new Date(item[1].savedAt); - // const diffMs = new Date() - saveTime; - // const diffSecs = Math.floor(diffMs / 1000); + const saveTime = new Date(item.savedAt); + const diffMs = new Date() - saveTime; + const diffSecs = Math.floor(diffMs / 1000); - // let diffString = `about ${diffSecs} seconds ago`; + let diffString = `about ${diffSecs} seconds ago`; - // if(diffSecs > 60) diffString = `about ${Math.floor(diffSecs / 60)} minutes ago`; - // if(diffSecs > (60 * 60)) diffString = `about ${Math.floor(diffSecs / (60 * 60))} hours ago`; - // if(diffSecs > (24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (24 * 60 * 60))} days ago`; - // if(diffSecs > (7 * 24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (7 * 24 * 60 * 60))} weeks ago`; + if(diffSecs > 60) diffString = `about ${Math.floor(diffSecs / 60)} minutes ago`; + if(diffSecs > (60 * 60)) diffString = `about ${Math.floor(diffSecs / (60 * 60))} hours ago`; + if(diffSecs > (24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (24 * 60 * 60))} days ago`; + if(diffSecs > (7 * 24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (7 * 24 * 60 * 60))} weeks ago`; - // return
{this.replaceContent(item);}} > - // - // v{item.version} : {diffString} - //
; - // })} - //
; + return
{this.replaceContent(item);}} > + + v{item.version} : {diffString} +
; + })} +
; }, renderEditorButtons : function(){ @@ -202,9 +216,10 @@ const Snippetbar = createClass({ } return
-
+
- {/* { this.renderHistoryItems() } */} + { this.state.showHistory && this.renderHistoryItems() }
From 1e38ed8d1f1a0e7eb621dd6b570787d67eb7928a Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 20:53:05 +1300 Subject: [PATCH 04/21] Bump max-lines --- client/homebrew/editor/snippetbar/snippetbar.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 29ce4d8c8..503596236 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -1,4 +1,4 @@ -/*eslint max-lines: ["warn", {"max": 250, "skipBlankLines": true, "skipComments": true}]*/ +/*eslint max-lines: ["warn", {"max": 350, "skipBlankLines": true, "skipComments": true}]*/ require('./snippetbar.less'); const React = require('react'); const createClass = require('create-react-class'); From 24bffacaeb4775fb4cab29533fee8359c29ca5d1 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 20:53:32 +1300 Subject: [PATCH 05/21] GC functional, config values now actually used --- client/homebrew/utils/versionHistory.js | 40 +++++++++++++++---------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index b72ab6db6..9dfeab8fd 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -15,9 +15,8 @@ const DEFAULT_HISTORY_SAVE_DELAYS = { const DEFAULT_GARBAGE_COLLECT_DELAY = 28 * 24 * 60; -const HISTORY_SAVE_DELAYS = global.config?.historyData?.HISTORY_SAVE_DELAYS ?? DEFAULT_HISTORY_SAVE_DELAYS; -const GARBAGE_COLLECT_DELAY = global.config?.historyData?.GARBAGE_COLLECT_DELAY ?? DEFAULT_GARBAGE_COLLECT_DELAY; - +let HISTORY_SAVE_DELAYS = DEFAULT_HISTORY_SAVE_DELAYS; +let GARBAGE_COLLECT_DELAY = DEFAULT_GARBAGE_COLLECT_DELAY; function getKeyBySlot(brew, slot){ @@ -48,6 +47,10 @@ function parseBrewForStorage(brew, slot = 0) { expireAt : new Date() }; + if(global.config?.history?.HISTORY_SAVE_DELAYS){ + HISTORY_SAVE_DELAYS = global.config?.history?.HISTORY_SAVE_DELAYS; + } + archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]); const key = getKeyBySlot(brew, slot); @@ -128,17 +131,22 @@ export async function updateHistory(brew) { }; }; -export function versionHistoryGarbageCollection(){ - console.log('Version History Garbage Collection'); - // Object.keys(IDB) - // .filter((key)=>{ - // return key.startsWith(HISTORY_PREFIX); - // }) - // .forEach((key)=>{ - // const collectAt = new Date(JSON.parse(IDB.get(key)).savedAt); - // collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); - // if(new Date() > collectAt){ - // IDB.removeItem(key); - // } - // }); +export async function versionHistoryGarbageCollection(){ + if(global.config?.history?.GARBAGE_COLLECT_DELAY != GARBAGE_COLLECT_DELAY) GARBAGE_COLLECT_DELAY = global.config?.history?.GARBAGE_COLLECT_DELAY; + + await IDB.entries() + .then((entries)=>{ + entries.forEach((entry)=>{ + const key = entry[0]; + const value = entry[1]; + + if(key.startsWith(`${HISTORY_PREFIX}`)) { + const collectAt = new Date(value.savedAt); + collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); + if(new Date() > collectAt){ + IDB.del(key); + }; + } + }); + }); }; \ No newline at end of file From 5d9ef3fa6c3ea78c86b410076439ceb4de0740bc Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 21:53:05 +1300 Subject: [PATCH 06/21] Add custom IDB store --- client/homebrew/utils/versionHistory.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 9dfeab8fd..396a47846 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -13,6 +13,9 @@ const DEFAULT_HISTORY_SAVE_DELAYS = { '5' : 2 * 24 * 60 }; +const HB_DB = 'HOMEBREWERY-DB'; +const HB_STORE = 'HISTORY'; + const DEFAULT_GARBAGE_COLLECT_DELAY = 28 * 24 * 60; let HISTORY_SAVE_DELAYS = DEFAULT_HISTORY_SAVE_DELAYS; @@ -59,9 +62,13 @@ function parseBrewForStorage(brew, slot = 0) { } +async function createHBStore(){ + return await IDB.createStore(HB_DB, HB_STORE); +} + export async function historyCheck(brew){ if(!IDB) return false; - const historyExists = await IDB.keys() + const historyExists = await IDB.keys(await createHBStore()) .then((keys)=>{ return [...keys].some((key)=>{ return key.startsWith(`${HISTORY_PREFIX}-${brew.shareId}`); @@ -84,7 +91,7 @@ export async function loadHistory(brew){ const history = []; // Load all keys from IDB at once - await IDB.getMany(historyKeys) + await IDB.getMany(historyKeys, await createHBStore()) .then((dataArray)=>{ return dataArray.forEach((data)=>{ history.push(data ?? DEFAULT_HISTORY_ITEM); @@ -123,7 +130,7 @@ export async function updateHistory(brew) { // Update the most recent brew historyUpdate.push(parseBrewForStorage(brew, 1)); - IDB.setMany(historyUpdate); + IDB.setMany(historyUpdate, await createHBStore()); // Break out of data checks because we found an expired value break; @@ -134,7 +141,7 @@ export async function updateHistory(brew) { export async function versionHistoryGarbageCollection(){ if(global.config?.history?.GARBAGE_COLLECT_DELAY != GARBAGE_COLLECT_DELAY) GARBAGE_COLLECT_DELAY = global.config?.history?.GARBAGE_COLLECT_DELAY; - await IDB.entries() + await IDB.entries(await createHBStore()) .then((entries)=>{ entries.forEach((entry)=>{ const key = entry[0]; From 7e165c6e61b7402ca712bf5c1e2e6deb494f4f17 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 21:53:24 +1300 Subject: [PATCH 07/21] Remove unnecessary key check --- client/homebrew/utils/versionHistory.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 396a47846..a4b38911e 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -143,17 +143,17 @@ export async function versionHistoryGarbageCollection(){ await IDB.entries(await createHBStore()) .then((entries)=>{ - entries.forEach((entry)=>{ + entries.forEach(async (entry)=>{ const key = entry[0]; const value = entry[1]; - if(key.startsWith(`${HISTORY_PREFIX}`)) { - const collectAt = new Date(value.savedAt); - collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); - if(new Date() > collectAt){ - IDB.del(key); - }; - } + // if(key.startsWith(`${HISTORY_PREFIX}`)) { // This check was to make sure we were only selecting the history keys, should be unnecessary + const collectAt = new Date(value.savedAt); + collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); + if(new Date() > collectAt){ + IDB.del(key, await createHBStore()); + }; + // } }); }); }; \ No newline at end of file From e75eb72d3f7552dec462844b27383a2feb2a55a0 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 14:54:23 +1300 Subject: [PATCH 08/21] Remove obsolete function --- client/homebrew/utils/versionHistory.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index a4b38911e..49239bd94 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -27,16 +27,6 @@ function getKeyBySlot(brew, slot){ return `${HISTORY_PREFIX}-${brew.shareId}-${slot}`; }; -// function getVersionBySlot(brew, slot){ -// // Read stored brew data -// // - If it exists, parse data to object -// // - If it doesn't exist, pass default object -// const key = getKeyBySlot(brew, slot); -// const storedVersion = IDB.get(key); -// const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; -// return output; -// }; - function parseBrewForStorage(brew, slot = 0) { // Strip out unneeded object properties // Returns an array of [ key, brew ] From 291e16b124826d0e8ada513cda243fa6af12799f Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 14:57:04 +1300 Subject: [PATCH 09/21] Revert to use local consts instead of config vars --- client/homebrew/utils/versionHistory.js | 12 ++---------- server/app.js | 3 +-- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 49239bd94..9f026c41c 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -4,7 +4,7 @@ export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'; export const HISTORY_SLOTS = 5; // History values in minutes -const DEFAULT_HISTORY_SAVE_DELAYS = { +const HISTORY_SAVE_DELAYS = { '0' : 0, '1' : 2, '2' : 10, @@ -16,10 +16,7 @@ const DEFAULT_HISTORY_SAVE_DELAYS = { const HB_DB = 'HOMEBREWERY-DB'; const HB_STORE = 'HISTORY'; -const DEFAULT_GARBAGE_COLLECT_DELAY = 28 * 24 * 60; - -let HISTORY_SAVE_DELAYS = DEFAULT_HISTORY_SAVE_DELAYS; -let GARBAGE_COLLECT_DELAY = DEFAULT_GARBAGE_COLLECT_DELAY; +const GARBAGE_COLLECT_DELAY = 28 * 24 * 60; function getKeyBySlot(brew, slot){ @@ -40,10 +37,6 @@ function parseBrewForStorage(brew, slot = 0) { expireAt : new Date() }; - if(global.config?.history?.HISTORY_SAVE_DELAYS){ - HISTORY_SAVE_DELAYS = global.config?.history?.HISTORY_SAVE_DELAYS; - } - archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]); const key = getKeyBySlot(brew, slot); @@ -129,7 +122,6 @@ export async function updateHistory(brew) { }; export async function versionHistoryGarbageCollection(){ - if(global.config?.history?.GARBAGE_COLLECT_DELAY != GARBAGE_COLLECT_DELAY) GARBAGE_COLLECT_DELAY = global.config?.history?.GARBAGE_COLLECT_DELAY; await IDB.entries(await createHBStore()) .then((entries)=>{ diff --git a/server/app.js b/server/app.js index 6dcabec12..90adde370 100644 --- a/server/app.js +++ b/server/app.js @@ -475,8 +475,7 @@ const renderPage = async (req, res)=>{ const configuration = { local : isLocalEnvironment, publicUrl : config.get('publicUrl') ?? '', - environment : nodeEnv, - history : config.get('historyConfig') ?? {} + environment : nodeEnv }; const props = { version : require('./../package.json').version, From 2a9402634f062925a14dfa2aa6df3a07505a27dd Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 14:58:20 +1300 Subject: [PATCH 10/21] Remove unnecessary guard clause --- client/homebrew/utils/versionHistory.js | 1 - 1 file changed, 1 deletion(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 9f026c41c..bf987bb1f 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -50,7 +50,6 @@ async function createHBStore(){ } export async function historyCheck(brew){ - if(!IDB) return false; const historyExists = await IDB.keys(await createHBStore()) .then((keys)=>{ return [...keys].some((key)=>{ From aa68762294700df2f625b9407af982d320523a04 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 15:59:11 +1300 Subject: [PATCH 11/21] Simplify historyCheck logic --- client/homebrew/utils/versionHistory.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index bf987bb1f..387ff1f5a 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -44,21 +44,16 @@ function parseBrewForStorage(brew, slot = 0) { return [key, archiveBrew]; } - +// Create a custom IDB store async function createHBStore(){ return await IDB.createStore(HB_DB, HB_STORE); } +// Determine if history data exists for this brew export async function historyCheck(brew){ - const historyExists = await IDB.keys(await createHBStore()) - .then((keys)=>{ - return [...keys].some((key)=>{ - return key.startsWith(`${HISTORY_PREFIX}-${brew.shareId}`); - }); - }) + const keys = await IDB.keys(await createHBStore()) .catch(()=>{return false;}); - - return historyExists; + return keys.some((key)=>{return key.startsWith(`${HISTORY_PREFIX}-${brew.shareId}`);}); } export async function loadHistory(brew){ From 0b44e68a36588737029113aadb7602b5d5160116 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 17:27:33 +1300 Subject: [PATCH 12/21] Simplify garbage collection --- client/homebrew/utils/versionHistory.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 387ff1f5a..3be3a9aba 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -117,19 +117,13 @@ export async function updateHistory(brew) { export async function versionHistoryGarbageCollection(){ - await IDB.entries(await createHBStore()) - .then((entries)=>{ - entries.forEach(async (entry)=>{ - const key = entry[0]; - const value = entry[1]; + const entries = await IDB.entries(await createHBStore()); - // if(key.startsWith(`${HISTORY_PREFIX}`)) { // This check was to make sure we were only selecting the history keys, should be unnecessary - const collectAt = new Date(value.savedAt); - collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); - if(new Date() > collectAt){ - IDB.del(key, await createHBStore()); - }; - // } - }); - }); + for (const [key, value] of entries){ + const expireAt = new Date(value.savedAt); + expireAt.setMinutes(expireAt.getMinutes() + GARBAGE_COLLECT_DELAY); + if(new Date() > expireAt){ + IDB.del(key, await createHBStore()); + }; + }; }; \ No newline at end of file From fb2d03f5a2e01043d60ff3ade8575bfd447095f2 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 17:50:26 +1300 Subject: [PATCH 13/21] Change to test values --- client/homebrew/utils/versionHistory.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 3be3a9aba..5bc3e9771 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -4,19 +4,28 @@ export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'; export const HISTORY_SLOTS = 5; // History values in minutes +// const HISTORY_SAVE_DELAYS = { +// '0' : 0, +// '1' : 2, +// '2' : 10, +// '3' : 60, +// '4' : 12 * 60, +// '5' : 2 * 24 * 60 +// }; const HISTORY_SAVE_DELAYS = { '0' : 0, - '1' : 2, - '2' : 10, - '3' : 60, - '4' : 12 * 60, - '5' : 2 * 24 * 60 + '1' : 1, + '2' : 2, + '3' : 3, + '4' : 4, + '5' : 5 }; const HB_DB = 'HOMEBREWERY-DB'; const HB_STORE = 'HISTORY'; -const GARBAGE_COLLECT_DELAY = 28 * 24 * 60; +// const GARBAGE_COLLECT_DELAY = 28 * 24 * 60; +const GARBAGE_COLLECT_DELAY = 10; function getKeyBySlot(brew, slot){ From 5651c665620e7f9c9b9843768269a972cc8f09b1 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 18:29:15 +1300 Subject: [PATCH 14/21] Fix race condition --- client/homebrew/pages/editPage/editPage.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/homebrew/pages/editPage/editPage.jsx b/client/homebrew/pages/editPage/editPage.jsx index 79bb01aa2..fcc43e81a 100644 --- a/client/homebrew/pages/editPage/editPage.jsx +++ b/client/homebrew/pages/editPage/editPage.jsx @@ -228,8 +228,8 @@ const EditPage = createClass({ htmlErrors : Markdown.validate(prevState.brew.text) })); - updateHistory(this.state.brew); - versionHistoryGarbageCollection(); + await updateHistory(this.state.brew); + await versionHistoryGarbageCollection(); const transfer = this.state.saveGoogle == _.isNil(this.state.brew.googleId); From dc66d36b2dca49a3b109ca5257688ffb8e179344 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 9 Oct 2024 19:35:48 +1300 Subject: [PATCH 15/21] Simplify history loading --- client/homebrew/utils/versionHistory.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 5bc3e9771..362c1cb33 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -75,21 +75,9 @@ export async function loadHistory(brew){ historyKeys.push(getKeyBySlot(brew, i)); }; - const history = []; // Load all keys from IDB at once - await IDB.getMany(historyKeys, await createHBStore()) - .then((dataArray)=>{ - return dataArray.forEach((data)=>{ - history.push(data ?? DEFAULT_HISTORY_ITEM); - }); - }) - .catch(()=>{ - historyKeys.forEach(()=>{ - history.push(DEFAULT_HISTORY_ITEM); - }); - }); - - return history; + const dataArray = await IDB.getMany(historyKeys, await createHBStore()); + return dataArray.map((data)=>{ return data ?? DEFAULT_HISTORY_ITEM; }); } export async function updateHistory(brew) { From 8db12739d3ea1358c5db9d772860adb81770618a Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Thu, 10 Oct 2024 08:34:15 +1300 Subject: [PATCH 16/21] Remove obsolete function --- client/homebrew/utils/versionHistory.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 362c1cb33..33e4eb178 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -58,13 +58,6 @@ async function createHBStore(){ return await IDB.createStore(HB_DB, HB_STORE); } -// Determine if history data exists for this brew -export async function historyCheck(brew){ - const keys = await IDB.keys(await createHBStore()) - .catch(()=>{return false;}); - return keys.some((key)=>{return key.startsWith(`${HISTORY_PREFIX}-${brew.shareId}`);}); -} - export async function loadHistory(brew){ const DEFAULT_HISTORY_ITEM = { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; From 8f5b4215316af505b5ec227282de5e88ca95debf Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Thu, 10 Oct 2024 08:34:24 +1300 Subject: [PATCH 17/21] Fix infinite loop --- .../homebrew/editor/snippetbar/snippetbar.jsx | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 503596236..429dfd1b6 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -5,7 +5,7 @@ const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); -import { historyCheck, loadHistory } from '../../utils/versionHistory.js'; +import { loadHistory } from '../../utils/versionHistory.js'; //Import all themes const ThemeSnippets = {}; @@ -70,23 +70,27 @@ const Snippetbar = createClass({ }); }; - const checkHistoryExists = (await historyCheck(this.props.brew)) ?? false; - if(checkHistoryExists != prevState.historyExists){ + // Update history list if it has changed + const checkHistoryItems = await loadHistory(this.props.brew); + + // If all items have the noData property, there is no saved data + const checkHistoryExists = !checkHistoryItems.every((historyItem)=>{ + return historyItem?.noData; + }); + if(prevState.historyExists != checkHistoryExists){ this.setState({ historyExists : checkHistoryExists }); } - // Update history list if it has changed - const checkHistoryItems = await loadHistory(this.props.brew); - if(checkHistoryItems.some((historyItem, index)=>{ - return !index < this.state.historyItems.length || historyItem != this.state.historyItems[index]; + // If any history items have changed, update the list + if(checkHistoryExists && checkHistoryItems.some((historyItem, index)=>{ + return index >= prevState.historyItems.length || !_.isEqual(historyItem, prevState.historyItems[index]); })){ this.setState({ historyItems : checkHistoryItems }); } - }, mergeCustomizer : function(oldValue, newValue, key) { From 618de544bfb41090b38a63847ef6a7d8c1c91463 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Thu, 10 Oct 2024 08:43:38 +1300 Subject: [PATCH 18/21] Remove unnecessary check --- client/homebrew/editor/snippetbar/snippetbar.jsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 429dfd1b6..513b0ab0b 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -175,8 +175,6 @@ const Snippetbar = createClass({ }, renderHistoryItems : function() { - if(this.state.historyItems.length == 0) return; - return
{_.map(this.state.historyItems, (item, index)=>{ if(item.noData || !item.savedAt) return; From 36674f4cf2899c61ebdb367d76ba9b9eef2949b8 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Thu, 10 Oct 2024 08:48:14 +1300 Subject: [PATCH 19/21] Add explicit guard clause to renderHistoryItems --- client/homebrew/editor/snippetbar/snippetbar.jsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 513b0ab0b..d457d92f2 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -175,6 +175,8 @@ const Snippetbar = createClass({ }, renderHistoryItems : function() { + if(!this.state.historyExists) return; + return
{_.map(this.state.historyItems, (item, index)=>{ if(item.noData || !item.savedAt) return; From 0deb9073cd0af3f4a01ed57a9a176f36757d6e5a Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Thu, 10 Oct 2024 08:57:41 +1300 Subject: [PATCH 20/21] Remove obsolete file --- client/homebrew/utils/useIDB.js | 80 --------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 client/homebrew/utils/useIDB.js diff --git a/client/homebrew/utils/useIDB.js b/client/homebrew/utils/useIDB.js deleted file mode 100644 index 03404160a..000000000 --- a/client/homebrew/utils/useIDB.js +++ /dev/null @@ -1,80 +0,0 @@ -import { - get as iGet, - getMany as iGetMany, - set as iSet, - setMany as iSetMany, - update as iUpdate, - del as iDel, - keys, - createStore, - Store -} from 'idb-keyval/dist/index.js'; // EcmaScript Module - -const HOMEBREWERY_DB = 'HOMEBREWERY-DB'; -const HOMEBREWERY_STORE = 'HOMEBREWERY-STORE'; - -let hbStore; - -function init(){ - if(hbStore) return true; - if(!hbStore && typeof window != 'undefined' && typeof window.indexedDB != 'undefined'){ - hbStore = createStore(HOMEBREWERY_DB, HOMEBREWERY_STORE); - return true; - } - return false; -} - -function checkFn(fn){ - return init() && fn(); -}; - -const get = checkFn(async (key)=>{ - console.log('get:', key); - return iGet(key, hbStore); -}); - -const getMany = checkFn(async (keys)=>{ - checkFn(async ()=>{ - console.log('getMany:', keys); - return await iGetMany(keys, hbStore); - }); -}); - -const set = checkFn(async (key, val)=>{ - console.log('set:', key, val); - init(); - return iSet(key, val, hbStore); -}); - -const setMany = checkFn(async (keyValArray)=>{ - console.log('set:', keyValArray); - init(); - return iSetMany(keyValArray, hbStore); -}); - - -const update = checkFn(async (key, updateFn)=>{ - init(); - return iUpdate(key, updateFn, hbStore); -}); - -const remove = checkFn(async (key)=>{ - console.log('remove:', key); - init(); - return iDel(key, hbStore); -}); - -const list = checkFn(async ()=>{ - init(); - return await keys(hbStore); -}); - -module.exports = { - get, - getMany, - set, - setMany, - update, - remove, - list -}; \ No newline at end of file From ab6861675dbc6f28cbb61368d6027403fb62bee7 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Thu, 10 Oct 2024 09:07:54 +1300 Subject: [PATCH 21/21] Comment out history testing values --- client/homebrew/utils/versionHistory.js | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 33e4eb178..a23af844a 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -4,28 +4,28 @@ export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'; export const HISTORY_SLOTS = 5; // History values in minutes -// const HISTORY_SAVE_DELAYS = { -// '0' : 0, -// '1' : 2, -// '2' : 10, -// '3' : 60, -// '4' : 12 * 60, -// '5' : 2 * 24 * 60 -// }; const HISTORY_SAVE_DELAYS = { '0' : 0, - '1' : 1, - '2' : 2, - '3' : 3, - '4' : 4, - '5' : 5 + '1' : 2, + '2' : 10, + '3' : 60, + '4' : 12 * 60, + '5' : 2 * 24 * 60 }; +// const HISTORY_SAVE_DELAYS = { +// '0' : 0, +// '1' : 1, +// '2' : 2, +// '3' : 3, +// '4' : 4, +// '5' : 5 +// }; const HB_DB = 'HOMEBREWERY-DB'; const HB_STORE = 'HISTORY'; -// const GARBAGE_COLLECT_DELAY = 28 * 24 * 60; -const GARBAGE_COLLECT_DELAY = 10; +const GARBAGE_COLLECT_DELAY = 28 * 24 * 60; +// const GARBAGE_COLLECT_DELAY = 10; function getKeyBySlot(brew, slot){ @@ -97,7 +97,7 @@ export async function updateHistory(brew) { // Update the most recent brew historyUpdate.push(parseBrewForStorage(brew, 1)); - IDB.setMany(historyUpdate, await createHBStore()); + await IDB.setMany(historyUpdate, await createHBStore()); // Break out of data checks because we found an expired value break; @@ -113,7 +113,7 @@ export async function versionHistoryGarbageCollection(){ const expireAt = new Date(value.savedAt); expireAt.setMinutes(expireAt.getMinutes() + GARBAGE_COLLECT_DELAY); if(new Date() > expireAt){ - IDB.del(key, await createHBStore()); + await IDB.del(key, await createHBStore()); }; }; }; \ No newline at end of file