mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-05 03:42:38 +00:00
Updated version saving logic
This commit is contained in:
@@ -1,50 +1,68 @@
|
|||||||
const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'
|
export const HISTORY_PREFIX = 'HOMEBREWERY-HISTORY'
|
||||||
const HISTORY_SAVE_DELAYS = [
|
// const HISTORY_SAVE_DELAYS = {
|
||||||
2, // 2 minutes
|
// 0: 0, // 0 minutes (if not specified)
|
||||||
10, // 10 minutes
|
// 1: 2, // 2 minutes
|
||||||
60, // 60 minutes
|
// 2: 10, // 10 minutes
|
||||||
12 * 60, // 12 hours
|
// 3: 60, // 60 minutes
|
||||||
2 * 24 * 60 // 2 days
|
// 4: 12 * 60, // 12 hours
|
||||||
];
|
// 5: 2 * 24 * 60 // 2 days
|
||||||
const HISTORY_VERSION_DIFFS = [
|
// };
|
||||||
1,
|
|
||||||
10,
|
|
||||||
50,
|
|
||||||
100,
|
|
||||||
250
|
|
||||||
]
|
|
||||||
|
|
||||||
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 = {};
|
const archiveBrew = {};
|
||||||
|
|
||||||
|
// Data from brew to be stored
|
||||||
archiveBrew.title = brew.title;
|
archiveBrew.title = brew.title;
|
||||||
archiveBrew.text = brew.text;
|
archiveBrew.text = brew.text;
|
||||||
archiveBrew.style = brew.style;
|
archiveBrew.style = brew.style;
|
||||||
archiveBrew.savedAt = new Date();
|
|
||||||
archiveBrew.version = brew.version;
|
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));
|
localStorage.setItem(key, JSON.stringify(archiveBrew));
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function updateHistory(brew) {
|
export function updateHistory(brew) {
|
||||||
const historyKeys = [];
|
const numbers = [1,2,3,4,5];
|
||||||
[1,2,3,4,5].forEach((i)=>{
|
const historyKeys = {};
|
||||||
historyKeys.push(`${HISTORY_PREFIX}-${brew.shareId}-${i}`);
|
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);
|
const storedVersion = localStorage.getItem(key);
|
||||||
|
|
||||||
|
// If no version stored at this key, update and break
|
||||||
if(!storedVersion){
|
if(!storedVersion){
|
||||||
updateStoredBrew(key, brew);
|
console.log('Empty slot: ', slot);
|
||||||
return;
|
updateStoredBrew(key, brew, slot);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse slot data
|
||||||
const storedObject = JSON.parse(storedVersion);
|
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 slot has expired, update
|
||||||
|
if(new Date() >= new Date(storedObject.expireAt)){
|
||||||
if(new Date() >= targetTime && brew.version >= targetVersion){
|
console.log('Expired slot: ', slot);
|
||||||
updateStoredBrew(key, brew);
|
updateStoredBrew(key, brew, slot);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user