diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index 796078028..ad952f74b 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -1,50 +1,68 @@ -const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY' -const HISTORY_SAVE_DELAYS = [ - 2, // 2 minutes - 10, // 10 minutes - 60, // 60 minutes - 12 * 60, // 12 hours - 2 * 24 * 60 // 2 days -]; -const HISTORY_VERSION_DIFFS = [ - 1, - 10, - 50, - 100, - 250 -] +export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY' +// const HISTORY_SAVE_DELAYS = { +// 0: 0, // 0 minutes (if not specified) +// 1: 2, // 2 minutes +// 2: 10, // 10 minutes +// 3: 60, // 60 minutes +// 4: 12 * 60, // 12 hours +// 5: 2 * 24 * 60 // 2 days +// }; -function updateStoredBrew(key, brew){ +const HISTORY_SAVE_DELAYS = { + // Test values + 0: 0, // 0 minutes (if not specified) + 1: 1, // 1 minutes + 2: 2, // 2 minutes + 3: 3, // 3 minutes + 4: 4, // 4 minutes + 5: 5 // 5 minutes +}; + +function updateStoredBrew(key, brew, slot=0){ const archiveBrew = {}; + + // Data from brew to be stored archiveBrew.title = brew.title; archiveBrew.text = brew.text; archiveBrew.style = brew.style; - archiveBrew.savedAt = new Date(); archiveBrew.version = brew.version; + + // Calculated values + archiveBrew.savedAt = new Date(); + archiveBrew.expireAt = new Date(); + archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]); + + // Store data localStorage.setItem(key, JSON.stringify(archiveBrew)); return; }; export function updateHistory(brew) { - const historyKeys = []; - [1,2,3,4,5].forEach((i)=>{ - historyKeys.push(`${HISTORY_PREFIX}-${brew.shareId}-${i}`); + const numbers = [1,2,3,4,5]; + const historyKeys = {}; + numbers.forEach((i)=>{ + historyKeys[i] = `${HISTORY_PREFIX}-${brew.shareId}-${i}`; }); - historyKeys.forEach((key, i)=>{ + numbers.toReversed().every((slot)=>{ + const key = historyKeys[slot]; const storedVersion = localStorage.getItem(key); + + // If no version stored at this key, update and break if(!storedVersion){ - updateStoredBrew(key, brew); - return; + console.log('Empty slot: ', slot); + updateStoredBrew(key, brew, slot); + return false; } - + + // Parse slot data const storedObject = JSON.parse(storedVersion); - let targetTime = new Date(storedObject.savedAt); - targetTime.setMinutes(targetTime.getMinutes() + HISTORY_SAVE_DELAYS[i]); - const targetVersion = storedObject.version + HISTORY_VERSION_DIFFS[i]; - - if(new Date() >= targetTime && brew.version >= targetVersion){ - updateStoredBrew(key, brew); + // If slot has expired, update + if(new Date() >= new Date(storedObject.expireAt)){ + console.log('Expired slot: ', slot); + updateStoredBrew(key, brew, slot); + return false; } + return true; }); };