From 9679e5b1304f3b5c8c395e298d6fd129397b682e Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 7 Sep 2024 14:00:31 +1200 Subject: [PATCH] Add garbage collection function to remove version data after specified period without update --- client/homebrew/pages/editPage/editPage.jsx | 3 ++- client/homebrew/utils/versionHistory.js | 25 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/client/homebrew/pages/editPage/editPage.jsx b/client/homebrew/pages/editPage/editPage.jsx index 004dd714a..3ecf74d73 100644 --- a/client/homebrew/pages/editPage/editPage.jsx +++ b/client/homebrew/pages/editPage/editPage.jsx @@ -27,7 +27,7 @@ const Markdown = require('naturalcrit/markdown.js'); const { DEFAULT_BREW_LOAD } = require('../../../../server/brewDefaults.js'); const { printCurrentBrew, fetchThemeBundle } = require('../../../../shared/helpers.js'); -import { updateHistory } from '../../utils/versionHistory.js'; +import { updateHistory, versionHistoryGarbageCollection } from '../../utils/versionHistory.js'; const googleDriveIcon = require('../../googleDrive.svg'); @@ -205,6 +205,7 @@ const EditPage = createClass({ })); updateHistory(this.state.brew); + versionHistoryGarbageCollection(); const transfer = this.state.saveGoogle == _.isNil(this.state.brew.googleId); diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 5d0ce15c5..c70edbe5d 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -7,6 +7,11 @@ export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY' // 4: 12 * 60, // 12 hours // 5: 2 * 24 * 60 // 2 days // }; +// +// const GARBAGE_COLLECT_AT = 28 * 24 * 60; // 28 days + + +// <=================== TEST VALUES STARTS ===================> // Test values const HISTORY_SAVE_DELAYS = { @@ -17,6 +22,11 @@ const HISTORY_SAVE_DELAYS = { 4: 4, // 4 minutes 5: 5 // 5 minutes }; +const GARBAGE_COLLECT_DELAY = 10; // 10 minutes + +// <==================== TEST VALUES ENDS ====================> + + const DEFAULT_STORED_BREW = { shareId : 'default_brew', @@ -94,3 +104,18 @@ export function updateHistory(brew) { return true; }); }; + +export function versionHistoryGarbageCollection(){ + Object.keys(localStorage) + .filter((key)=>{ + return key.startsWith(HISTORY_PREFIX); + }) + .forEach((key)=>{ + const collectAt = new Date(JSON.parse(localStorage.getItem(key)).expireAt); + collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); + if(new Date() > collectAt){ + console.log('GARBAGE COLLECTION:', key); + localStorage.removeItem(key); + } + }); +}; \ No newline at end of file