0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-22 09:37:53 +00:00

Simplify logic

This commit is contained in:
G.Ambatte
2024-09-14 16:50:36 +12:00
parent b6bbed0e1b
commit c77d6e5fae

View File

@@ -1,4 +1,4 @@
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY';
// const HISTORY_SAVE_DELAYS = {
// 0: 0, // 0 minutes (if not specified)
// 1: 2, // 2 minutes
@@ -58,7 +58,7 @@ function updateStoredBrew(brew, slot=0){
archiveBrew.shareId = brew.shareId;
// Calculated values
archiveBrew.savedAt = new Date();
archiveBrew.savedAt = brew?.savedAt || new Date();
archiveBrew.expireAt = new Date();
archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]);
@@ -69,40 +69,31 @@ function updateStoredBrew(brew, slot=0){
};
export function updateHistory(brew) {
const numbers = [1,2,3,4,5];
const history = {};
// Load data from local storage to History object
numbers.forEach((i)=>{
for (let i = 1; i<=5; i++){
history[i] = getVersionBySlot(brew, i);
});
};
// Walk each version position
numbers.toReversed().every((slot)=>{
for (let slot = 5; slot>0; slot--){
const storedVersion = history[slot];
// If slot has expired, update all lower slots and break
if(new Date() >= new Date(storedVersion.expireAt)){
const slots = Array.from(Array(slot - 1).keys());
slots.toReversed().every((slot)=>{
const actualSlot = slot + 1;
// Move data from actualSlot to actualSlot + 1
updateStoredBrew({ ...history[actualSlot], shareId: brew.shareId }, actualSlot + 1);
// If first slot, fill with current data
if(actualSlot == 1) {
for (let updateSlot = slot - 1; updateSlot>0; updateSlot--){
// Move data from updateSlot to updateSlot + 1
updateStoredBrew({ ...history[updateSlot], shareId: brew.shareId }, updateSlot + 1);
};
// Update the most recent brew
updateStoredBrew(brew, 1);
// Break after updating first slot
return false;
}
// Continue loop to move data in remaining slots
return true;
});
// Break out of data checks because we found an expired value
return false;
break;
}
// Continue data checks because this one wasn't expired
return true;
});
};
};
export function versionHistoryGarbageCollection(){