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
Great enhancements! Your updated code looks solid and implements both undo and redo functionalities well using Vue composables and local storage.
Now, you're asking about limiting both the undoStack and redoStack to a maximum of 100 items. To do this in a clean and reusable way, I'd suggest creating a helper function like
pushWithLimit. This abstraction keeps your core logic tidy and manages boundaries consistently.
✅ Enhancement: Limiting Stacks to 100 Items
Here's the updated code with the limit logic added:
🔁
pushWithLimitHelperAdd this utility near your composable or in a shared utilities file:
function pushWithLimit<T>(stack: T[], item: T, max: number = 100): void { stack.push(item); if (stack.length > max) { stack.splice(0, stack.length - max); // Remove oldest items } }
📦 Enhanced Composable with Limited Stacks
import useInfoSections from "~/composables/Tenant/useInfoSections"; import { InfoComponent, InfoSection, InfoSubSection, } from "~/types/Tenant/InfoPages"; import { deepClone } from "~/utils/common/objects"; function pushWithLimit<T>(stack: T[], item: T, max: number = 100) { stack.push(item); if (stack.length > max) { stack.splice(0, stack.length - max); // Trim the oldest items } } 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) { pushWithLimit(undoStack.value, last_snap_shot); } last_snap_shot = deepClone(prevValue); redoStack.value = []; // Clear future history } }, { deep: true, }, ); const undoActiveBlock = async () => { const lastBlock = undoStack.value.pop(); if (!lastBlock) return; if (activeBlock.value) { pushWithLimit(redoStack.value, deepClone(activeBlock.value)); } 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; if (activeBlock.value) { pushWithLimit(undoStack.value, 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 }; }
✅ Benefits of Using
pushWithLimit- Prevents memory overgrowth in history
- Makes the undo/redo stacks predictable
- Keeps code DRY and easy to maintain
If you want to scale it further or apply to any reactive arrays tied to Vue components/composables, consider wrapping
pushWithLimitin a composable-friendly reactive utility!Let me know if you'd like an animated history preview or buttons integration example too.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.