import { watch, computed, WatchSource, getCurrentInstance, onMounted, onUnmounted, nextTick, reactive, } from 'vue'; export function explicitComputed(source: WatchSource, fn: () => T) { const v = reactive({ value: fn() }); watch(source, () => (v.value = fn())); return computed(() => v.value); } export function tryOnMounted(fn: () => void, sync = true) { if (getCurrentInstance()) { onMounted(fn); } else if (sync) { fn(); } else { nextTick(fn); } } export function tryOnUnmounted(fn: () => Promise | void) { if (getCurrentInstance()) { onUnmounted(fn); } } export function tryTsxEmit(fn: (_instance: any) => Promise | void) { const instance = getCurrentInstance(); if (instance) { fn.call(null, instance); } } export function isInSetup() { if (!getCurrentInstance()) { throw new Error('Please put useForm function in the setup function!'); } }