import { computed } from "vue"; import { useStore } from "vuex"; export function useSysDict() { type Callback = () => Record | Promise>; const store = useStore(); const callbacks: Record = {}; const normalize = (content: Record) => { if (content == null) return {}; return Object.fromEntries( Object.entries(content).map(([k, v]) => { const parsed = parseInt(v); return [k, isNaN(parsed) ? v : parsed]; }) ); }; const updateDict = async (dictIds: string[], registerCallbacks: Record = {}) => { await store.dispatch("getDictMap", dictIds); Object.assign(callbacks, registerCallbacks); for (const id of dictIds) { const cb = callbacks[id]; const resp = await cb?.(); if (resp) { store.commit("updateDict", [id, normalize(resp)]); } } }; const unregister = (ids: string[]) => { for (const id of ids) { delete callbacks[id]; } }; return { sysDict: computed>>(() => store.state.dict), updateDict, unregister, }; }