uni-ui-plus
Version:
🦄基于uni-ui的二次封装的uniapp组件库
36 lines (32 loc) • 712 B
text/typescript
import { isPromise } from './is'
export type Interceptor = (...args: any[]) => Promise<boolean> | boolean | undefined | void
export function funInterceptor(
interceptor: Interceptor | undefined,
{
args = [],
done,
canceled
}: {
args?: unknown[]
done: (val?: any) => void
canceled?: () => void
}
) {
if (interceptor) {
const returnVal = interceptor(undefined, ...args)
if (isPromise(returnVal)) {
returnVal
.then((value) => {
if (value) done(value)
else if (canceled) canceled()
})
.catch(() => {})
} else if (returnVal) {
done()
} else if (canceled) {
canceled()
}
} else {
done()
}
}