mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-17 12:23:21 +00:00
Simplify logic
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'
|
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY';
|
||||||
// const HISTORY_SAVE_DELAYS = {
|
// const HISTORY_SAVE_DELAYS = {
|
||||||
// 0: 0, // 0 minutes (if not specified)
|
// 0: 0, // 0 minutes (if not specified)
|
||||||
// 1: 2, // 2 minutes
|
// 1: 2, // 2 minutes
|
||||||
@@ -58,7 +58,7 @@ function updateStoredBrew(brew, slot=0){
|
|||||||
archiveBrew.shareId = brew.shareId;
|
archiveBrew.shareId = brew.shareId;
|
||||||
|
|
||||||
// Calculated values
|
// Calculated values
|
||||||
archiveBrew.savedAt = new Date();
|
archiveBrew.savedAt = brew?.savedAt || new Date();
|
||||||
archiveBrew.expireAt = new Date();
|
archiveBrew.expireAt = new Date();
|
||||||
archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]);
|
archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]);
|
||||||
|
|
||||||
@@ -69,40 +69,31 @@ function updateStoredBrew(brew, slot=0){
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function updateHistory(brew) {
|
export function updateHistory(brew) {
|
||||||
const numbers = [1,2,3,4,5];
|
|
||||||
const history = {};
|
const history = {};
|
||||||
|
|
||||||
// Load data from local storage to History object
|
// Load data from local storage to History object
|
||||||
numbers.forEach((i)=>{
|
for (let i = 1; i<=5; i++){
|
||||||
history[i] = getVersionBySlot(brew, i);
|
history[i] = getVersionBySlot(brew, i);
|
||||||
});
|
};
|
||||||
|
|
||||||
// Walk each version position
|
// Walk each version position
|
||||||
numbers.toReversed().every((slot)=>{
|
for (let slot = 5; slot>0; slot--){
|
||||||
const storedVersion = history[slot];
|
const storedVersion = history[slot];
|
||||||
|
|
||||||
// If slot has expired, update all lower slots and break
|
// If slot has expired, update all lower slots and break
|
||||||
if(new Date() >= new Date(storedVersion.expireAt)){
|
if(new Date() >= new Date(storedVersion.expireAt)){
|
||||||
const slots = Array.from(Array(slot - 1).keys());
|
for (let updateSlot = slot - 1; updateSlot>0; updateSlot--){
|
||||||
slots.toReversed().every((slot)=>{
|
// Move data from updateSlot to updateSlot + 1
|
||||||
const actualSlot = slot + 1;
|
updateStoredBrew({ ...history[updateSlot], shareId: brew.shareId }, updateSlot + 1);
|
||||||
// Move data from actualSlot to actualSlot + 1
|
};
|
||||||
updateStoredBrew({ ...history[actualSlot], shareId: brew.shareId }, actualSlot + 1);
|
|
||||||
// If first slot, fill with current data
|
// Update the most recent brew
|
||||||
if(actualSlot == 1) {
|
|
||||||
updateStoredBrew(brew, 1);
|
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
|
// 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(){
|
export function versionHistoryGarbageCollection(){
|
||||||
|
|||||||
Reference in New Issue
Block a user