import * as env from '../types/instance.resolver' export function prevent(e: Event | MouseEvent, handler?: Function) { e.preventDefault() return handler?.(e) } export function stop(e: Event | MouseEvent, handler?: Function) { e.preventDefault() e.stopPropagation() return handler?.(e) } /**根据条件返回不同参数**/ export function divineWherer(where: boolean, value: T, defaultValue?: T): T { return (where ? value : defaultValue) as T } /**延时方法**/ export function divineDelay(delay = 100, handler?: Function) { return new Promise(resolve => { const timeout = setTimeout(() => { handler?.() resolve(undefined) clearTimeout(timeout) }, delay) }) } /**条件函数执行**/ export async function divineHandler(where: boolean | Function, scope: env.Omix<{ handler: Function; failure?: Function }>): Promise { if (typeof where === 'function') { where = await where() } if (where) { return await scope.handler() } else { return (await scope.failure?.()) ?? undefined } }