import { computed, shallowReactive, isRef, watch } from "vue"; import type { ComputedRef, UnwrapRef } from "vue"; export type Stack = { push: (raw: T) => () => void; pop: () => T; length: ComputedRef; current: ComputedRef; }; // 栈构建 export const stackFactory = (initVal?: T, debug?: boolean): Stack => { const stack = shallowReactive([]) as Array; if (initVal !== void 0) { stack.push(initVal); } return { push(raw: T) { stack.push(raw); if (debug) { console.warn('push', raw) } return () => { const index = stack.indexOf(raw); ~index && stack.splice(index, 1); if (debug) { console.warn('pop', raw) console.warn('current', stack) } }; }, pop() { let ret = stack[stack.length-- - 1]; if (debug) { console.warn('pop current', stack) } return ret; }, current: computed(() => { return stack[stack.length - 1]; }), length: computed(() => stack.length), }; }; export const flatStacksValue = }>( stacks: T ) => { const result = {} as { [key in keyof T]: UnwrapRef; }; const keys = Object.keys(stacks) as Array; const proxy = new Proxy(result, { get(_, key: keyof T) { if (keys.includes(key)) { return isRef(stacks[key].current.value) ? (stacks[key].current.value as any).value : stacks[key].current.value; } else { return stacks[key]; } }, set(_, key: keyof T, val) { if (isRef(stacks[key].current.value)) { (stacks[key].current.value as any).value = val; } else { (stacks[key].current.value as any) = val; } return true; }, }); return proxy; };