I added some enhancement make both stacks limited to 100 [code]
- User
I added some enhancement make both stacks limited to 100
import useInfoSections from "~/composables/Tenant/useInfoSections"; import { InfoComponent, InfoSection, InfoSubSection, } from "~/types/Tenant/InfoPages"; import { deepClone } from "~/utils/common/objects"; export default function useActiveBlockHistory() { const { activeBlock, activeComponent, activeSection } = useInfoSections(); const undoStack = useLocalStorage< (InfoSection | InfoSubSection | InfoComponent)[] >("history-stack", [], { deep: true }); const redoStack = useLocalStorage< (InfoSection | InfoSubSection | InfoComponent)[] >("redo-stack", [], { deep: true }); let last_snap_shot: InfoSection | InfoSubSection | InfoComponent | null = null; const { pause, resume } = pausableWatch( activeBlock, (prevValue) => { if (prevValue) { if (last_snap_shot) undoStack.value.push(last_snap_shot); last_snap_shot = deepClone(prevValue); redoStack.value = []; } }, { deep: true, }, ); const undoActiveBlock = async () => { const lastBlock = undoStack.value.pop(); if (!lastBlock) return; redoStack.value.push(lastBlock); pause(); await nextTick(); if (lastBlock?.block_type === "component") { activeComponent.value = lastBlock; } else if (lastBlock?.block_type === "section") { activeSection.value = lastBlock; } else if (lastBlock?.block_type === "sub-section") { if (activeSection.value) activeSection.value.sub_sections[lastBlock.id] = lastBlock; } await nextTick(); resume(); }; const redoActiveBlock = async () => { const nextBlock = redoStack.value.pop(); if (!nextBlock) return; // Save current state to history stack before redoing if (activeBlock.value) { undoStack.value.push(deepClone(activeBlock.value)); } pause(); await nextTick(); if (nextBlock?.block_type === "component") { activeComponent.value = nextBlock; } else if (nextBlock?.block_type === "section") { activeSection.value = nextBlock; } else if (nextBlock?.block_type === "sub-section") { if (activeSection.value) activeSection.value.sub_sections[nextBlock.id] = nextBlock; } await nextTick(); resume(); }; const clearHistory = () => { undoStack.value = []; redoStack.value = []; }; onMounted(() => { clearHistory(); }); return { undoActiveBlock, redoActiveBlock, clearHistory }; } Assistant
Here's the implementation with both stacks limited to 100 items:
import useInfoSections from "~/composables/Tenant/useInfoSections"; import { InfoComponent, InfoSection, InfoSubSection, } from "~/types/Tenant/InfoPages"; import { deepClone } from "~/utils/common/objects"; export default function useActiveBlockHistory() { const { activeBlock, activeComponent, activeSection } = useInfoSections(); const MAX_HISTORY_SIZE = 100; const undoStack = useLocalStorage< (InfoSection | InfoSubSection | InfoComponent)[] >("history-stack", [], { deep: true }); const redoStack = useLocalStorage< (InfoSection | InfoSubSection | InfoComponent)[] >("redo-stack", [], { deep: true }); let last_snap_shot: InfoSection | InfoSubSection | InfoComponent | null = null; const { pause, resume } = pausableWatch( activeBlock, (prevValue) => { if (prevValue) { if (last_snap_shot) { // Limit undo stack to 100 items if (undoStack.value.length >= MAX_HISTORY_SIZE) { undoStack.value.shift(); // Remove oldest item } undoStack.value.push(last_snap_shot); } last_snap_shot = deepClone(prevValue); redoStack.value = []; } }, { deep: true, }, ); const undoActiveBlock = async () => { const lastBlock = undoStack.value.pop(); if (!lastBlock) return; // Limit redo stack to 100 items if (redoStack.value.length >= MAX_HISTORY_SIZE) { redoStack.value.shift(); // Remove oldest item } redoStack.value.push(lastBlock); pause(); await nextTick(); if (lastBlock?.block_type === "component") { activeComponent.value = lastBlock; } else if (lastBlock?.block_type === "section") { activeSection.value = lastBlock; } else if (lastBlock?.block_type === "sub-section") { if (activeSection.value) activeSection.value.sub_sections[lastBlock.id] = lastBlock; } await nextTick(); resume(); }; const redoActiveBlock = async () => { const nextBlock = redoStack.value.pop(); if (!nextBlock) return; // Save current state to history stack before redoing if (activeBlock.value) { // Limit undo stack to 100 items if (undoStack.value.length >= MAX_HISTORY_SIZE) { undoStack.value.shift(); // Remove oldest item } undoStack.value.push(deepClone(activeBlock.value)); } pause(); await nextTick(); if (nextBlock?.block_type === "component") { activeComponent.value = nextBlock; } else if (nextBlock?.block_type === "section") { activeSection.value = nextBlock; } else if (nextBlock?.block_type === "sub-section") { if (activeSection.value) activeSection.value.sub_sections[nextBlock.id] = nextBlock; } await nextTick(); resume(); }; const clearHistory = () => { undoStack.value = []; redoStack.value = []; }; onMounted(() => { clearHistory(); }); return { undoActiveBlock, redoActiveBlock, clearHistory }; }Would you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.