chore: adjust callInterceptor params (#9674)

* chore: adjust callInterceptor params

* chore: upd
This commit is contained in:
neverland 2021-10-14 10:21:09 +08:00 committed by GitHub
parent 2bd6bc48c2
commit 66cf1dabba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 46 deletions

View File

@ -74,10 +74,7 @@ export default defineComponent({
const close = (action: DialogAction) => {
updateShow(false);
if (props.callback) {
props.callback(action);
}
props.callback?.(action);
};
const getActionHandler = (action: DialogAction) => () => {
@ -90,8 +87,7 @@ export default defineComponent({
if (props.beforeClose) {
loading[action] = true;
callInterceptor({
interceptor: props.beforeClose,
callInterceptor(props.beforeClose, {
args: [action],
done() {
close(action);

View File

@ -49,7 +49,7 @@ Dialog.defaultOptions = {
className: '',
allowHtml: false,
lockScroll: true,
transition: 'van-dialog-bounce',
transition: undefined,
beforeClose: null,
overlayClass: '',
overlayStyle: undefined,

View File

@ -100,8 +100,7 @@ export default defineComponent({
const updateShow = (show: boolean) => emit('update:show', show);
const emitClose = () => {
callInterceptor({
interceptor: props.beforeClose,
callInterceptor(props.beforeClose, {
args: [state.active],
done: () => updateShow(false),
});

View File

@ -113,8 +113,7 @@ export default defineComponent({
const close = () => {
if (opened) {
callInterceptor({
interceptor: props.beforeClose,
callInterceptor(props.beforeClose, {
done() {
opened = false;
emit('close');

View File

@ -128,9 +128,8 @@ export default defineComponent({
const setValue = (value: string | number) => {
if (props.beforeChange) {
callInterceptor({
callInterceptor(props.beforeChange, {
args: [value],
interceptor: props.beforeChange,
done() {
current.value = value;
},

View File

@ -162,8 +162,7 @@ export default defineComponent({
emit('click', position);
if (opened && !lockClick) {
callInterceptor({
interceptor: props.beforeClose,
callInterceptor(props.beforeClose, {
args: [
{
name: props.name,

View File

@ -83,8 +83,7 @@ export default defineComponent({
const setActive = (active: number | string) => {
if (active !== props.modelValue) {
callInterceptor({
interceptor: props.beforeChange,
callInterceptor(props.beforeChange, {
args: [active],
done() {
emit('update:modelValue', active);

View File

@ -307,8 +307,7 @@ export default defineComponent({
// should be removed in next major version
emit('disabled', name, title);
} else {
callInterceptor({
interceptor: props.beforeChange,
callInterceptor(props.beforeChange, {
args: [name],
done: () => {
setCurrentIndex(index);

View File

@ -60,8 +60,7 @@ export default defineComponent({
const onDelete = (event: MouseEvent) => {
const { name, item, index, beforeDelete } = props;
event.stopPropagation();
callInterceptor({
interceptor: beforeDelete,
callInterceptor(beforeDelete, {
args: [item, { name, index }],
done: () => emit('delete'),
});

View File

@ -3,17 +3,21 @@ import { isPromise } from './validate';
export type Interceptor = (...args: any[]) => Promise<boolean> | boolean;
export function callInterceptor(options: {
interceptor?: Interceptor;
args?: any[];
done: () => void;
canceled?: () => void;
}) {
const { interceptor, args, done, canceled } = options;
export function callInterceptor(
interceptor: Interceptor | undefined,
{
args = [],
done,
canceled,
}: {
args?: any[];
done: () => void;
canceled?: () => void;
}
) {
if (interceptor) {
// eslint-disable-next-line prefer-spread
const returnVal = interceptor.apply(null, args || []);
const returnVal = interceptor.apply(null, args);
if (isPromise(returnVal)) {
returnVal

View File

@ -3,50 +3,47 @@ import { callInterceptor } from '../interceptor';
test('callInterceptor', async () => {
const done = jest.fn();
callInterceptor({ done });
callInterceptor(undefined, { done });
expect(done).toHaveBeenCalledTimes(1);
callInterceptor({
interceptor: () => false,
callInterceptor(() => false, {
done,
});
expect(done).toHaveBeenCalledTimes(1);
callInterceptor({
interceptor: () => true,
callInterceptor(() => true, {
done,
});
expect(done).toHaveBeenCalledTimes(2);
callInterceptor({
interceptor: () => Promise.resolve(false),
callInterceptor(() => Promise.resolve(false), {
done,
});
await later();
expect(done).toHaveBeenCalledTimes(2);
callInterceptor({
interceptor: () => Promise.resolve(true),
callInterceptor(() => Promise.resolve(true), {
done,
});
await later();
expect(done).toHaveBeenCalledTimes(3);
callInterceptor({
interceptor: () => Promise.reject(),
callInterceptor(() => Promise.reject(), {
done,
});
await later();
expect(done).toHaveBeenCalledTimes(3);
callInterceptor({
interceptor: (...args) => {
callInterceptor(
(...args) => {
expect(args).toEqual(['foo']);
return false;
},
args: ['foo'],
done,
});
{
args: ['foo'],
done,
}
);
expect(done).toHaveBeenCalledTimes(3);
});