import { reactive, toRefs, nextTick, type UnwrapNestedRefs } from 'vue' export type NewState = Partial | ((e: UnwrapNestedRefs) => Partial) export function useState(props: T) { const state = reactive(Object.assign({ ...props })) function setState(newState: NewState, handler?: (e: typeof state) => void): Promise> { return new Promise(resolve => { if (typeof newState === 'function') { const _newState = newState(state) Object.assign(state, _newState) } else if (typeof newState === 'object') { Object.assign(state, newState) } nextTick(() => { handler?.(state) resolve(state) }) }) } return { ...toRefs(state), state, setState } }