Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:事件动作增加循环调用检查 #7074

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/amis-core/src/SchemaRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import LazyComponent from './components/LazyComponent';
import {
filterSchema,
loadRenderer,
RendererComponent,
RendererConfig,
RendererEnv,
RendererProps,
Expand All @@ -17,7 +18,12 @@ import {DebugWrapper} from './utils/debug';
import getExprProperties from './utils/filter-schema';
import {anyChanged, chainEvents, autobind} from './utils/helper';
import {SimpleMap} from './utils/SimpleMap';
import {bindEvent, dispatchEvent, RendererEvent} from './utils/renderer-event';
import {
bindEvent,
checkCircular,
dispatchEvent,
RendererEvent
} from './utils/renderer-event';
import {isAlive} from 'mobx-state-tree';
import {reaction} from 'mobx';
import {resolveVariableAndFilter} from './utils/tpl-builtin';
Expand Down Expand Up @@ -221,8 +227,18 @@ export class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
async dispatchEvent(
e: React.MouseEvent<any>,
data: any,
renderer?: React.Component<RendererProps> // for didmount
renderer?: React.Component // for didmount
): Promise<RendererEvent<any> | void> {
// 检查是否存在循环调用
const eventName = typeof e === 'string' ? e : e.type;
const action = (renderer?.constructor as RendererComponent)
?.circularEventAction?.[eventName];

if (action && !checkCircular(eventName, action, renderer?.props)) {
this.props?.env.notify('warning', '警告:事件动作配置存在循环调用!');
return;
}

return await dispatchEvent(
e,
this.cRef || renderer,
Expand Down
1 change: 1 addition & 0 deletions packages/amis-core/src/factory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface RendererProps

export type RendererComponent = React.ComponentType<RendererProps> & {
propsList?: Array<any>;
circularEventAction?: {[eventName: string]: string};
};

export interface RendererConfig extends RendererBasicConfig {
Expand Down
45 changes: 33 additions & 12 deletions packages/amis-core/src/renderers/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,15 @@ export default class Form extends React.Component<FormProps, object> {
'multiple'
];

static circularEventAction: {[eventName: string]: string} = {
inited: 'reload',
submit: 'submit',
submitSucc: 'submit',
submitFail: 'submit',
validateSucc: 'validate',
validateError: 'validate'
};

hooks: {
[propName: string]: Array<() => Promise<any>>;
} = {};
Expand Down Expand Up @@ -672,7 +681,8 @@ export default class Form extends React.Component<FormProps, object> {
responseData: value?.data ?? {},
responseStatus: store.error ? 1 : 0,
responseMsg: store.msg
})
}),
this
);

return value;
Expand Down Expand Up @@ -832,9 +842,9 @@ export default class Form extends React.Component<FormProps, object> {
.validate(this.hooks['validate'] || [], forceValidate)
.then((result: boolean) => {
if (result) {
dispatchEvent('validateSucc', data);
dispatchEvent('validateSucc', data, this);
} else {
dispatchEvent('validateError', data);
dispatchEvent('validateError', data, this);
}
return result;
});
Expand Down Expand Up @@ -866,7 +876,7 @@ export default class Form extends React.Component<FormProps, object> {
submit(fn?: (values: object) => Promise<any>): Promise<any> {
const {store, messages, translate: __, dispatchEvent, data} = this.props;
this.flush();
const validateErrCb = () => dispatchEvent('validateError', data);
const validateErrCb = () => dispatchEvent('validateError', data, this);
return store.submit(
fn,
this.hooks['validate'] || [],
Expand Down Expand Up @@ -1082,7 +1092,8 @@ export default class Form extends React.Component<FormProps, object> {
if (!validationRes) {
const dispatcher = await dispatchEvent(
'validateError',
this.props.data
this.props.data,
this
);
if (!dispatcher?.prevented) {
env.notify('error', __('Form.validateFailed'));
Expand All @@ -1105,7 +1116,7 @@ export default class Form extends React.Component<FormProps, object> {
// 配了submit事件的表示将提交逻辑全部托管给事件
const {dispatchEvent, onEvent} = this.props;
const submitEvent = onEvent?.submit?.actions?.length;
const dispatcher = await dispatchEvent('submit', this.props.data);
const dispatcher = await dispatchEvent('submit', this.props.data, this);
if (dispatcher?.prevented || submitEvent) {
return;
}
Expand All @@ -1123,11 +1134,15 @@ export default class Form extends React.Component<FormProps, object> {
return Promise.resolve(false);
}
// 走到这里代表校验成功了
dispatchEvent('validateSucc', this.props.data);
dispatchEvent('validateSucc', this.props.data, this);

if (target) {
this.submitToTarget(filterTarget(target, values), values);
dispatchEvent('submitSucc', createObject(this.props.data, values));
dispatchEvent(
'submitSucc',
createObject(this.props.data, values),
this
);
} else if (action.actionType === 'reload') {
action.target &&
this.reloadTarget(filterTarget(action.target, values), values);
Expand All @@ -1150,7 +1165,8 @@ export default class Form extends React.Component<FormProps, object> {
// result为提交接口返回的内容
const dispatcher = await dispatchEvent(
'submitSucc',
createObject(this.props.data, {result})
createObject(this.props.data, {result}),
this
);
if (
!isEffectiveApi(finnalAsyncApi, store.data) ||
Expand All @@ -1168,7 +1184,7 @@ export default class Form extends React.Component<FormProps, object> {
checkInterval
).then((value: any) => {
// 派发asyncApiFinished事件
dispatchEvent('asyncApiFinished', store.data);
dispatchEvent('asyncApiFinished', store.data, this);
});
return {
cbResult,
Expand All @@ -1178,7 +1194,8 @@ export default class Form extends React.Component<FormProps, object> {
onFailed: async (result: Payload) => {
const dispatcher = await dispatchEvent(
'submitFail',
createObject(this.props.data, {error: result})
createObject(this.props.data, {error: result}),
this
);
return {
dispatcher
Expand All @@ -1205,7 +1222,11 @@ export default class Form extends React.Component<FormProps, object> {
});
} else {
// type为submit,但是没有配api以及target时,只派发事件
dispatchEvent('submitSucc', createObject(this.props.data, values));
dispatchEvent(
'submitSucc',
createObject(this.props.data, values),
this
);
}

return Promise.resolve(null);
Expand Down
14 changes: 14 additions & 0 deletions packages/amis-core/src/utils/renderer-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,18 @@ export const resolveEventData = (
);
};

export const checkCircular = (
eventName: string,
actionType: string,
props: any
) => {
const isCircular = props?.onEvent?.[eventName]?.actions?.some(
(item: any) =>
item.actionType === actionType &&
(item.componentId === props?.id || item.componentName === props?.name)
);

return !isCircular;
};

export default {};
21 changes: 7 additions & 14 deletions packages/amis/src/renderers/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import {Renderer, RendererProps, filterTarget} from 'amis-core';
import {observer} from 'mobx-react';
import {ServiceStore, IServiceStore} from 'amis-core';
import {
Api,
SchemaNode,
ActionObject,
Location,
ApiObject,
FunctionPropertyNames
} from 'amis-core';
import {Api, ActionObject, Location} from 'amis-core';
import {filter, evalExpression} from 'amis-core';
import {
isVisible,
Expand All @@ -34,9 +25,6 @@ import {
SchemaMessage
} from '../Schema';
import {SchemaRemark} from './Remark';
import {onAction} from 'mobx-state-tree';
import mapValues from 'lodash/mapValues';
import {resolveVariable} from 'amis-core';
import {buildStyle} from 'amis-core';
import {PullRefresh} from 'amis-ui';
import {scrollPosition, isMobile} from 'amis-core';
Expand Down Expand Up @@ -271,6 +259,10 @@ export default class Page extends React.Component<PageProps> {
'showErrorMsg'
];

static circularEventAction: {[eventName: string]: string} = {
inited: 'reload'
};

constructor(props: PageProps) {
super(props);

Expand Down Expand Up @@ -698,7 +690,8 @@ export default class Page extends React.Component<PageProps> {
responseStatus:
value?.status === undefined ? (store?.error ? 1 : 0) : value?.status,
responseMsg: value?.msg || store?.msg
})
}),
this
);

interval &&
Expand Down
28 changes: 19 additions & 9 deletions packages/amis/src/renderers/Service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ export default class Service extends React.Component<ServiceProps> {

static propsList: Array<string> = [];

static circularEventAction: {[eventName: string]: string} = {
fetchInited: 'reload',
fetchSchemaInited: 'reload'
};

constructor(props: ServiceProps) {
super(props);

Expand Down Expand Up @@ -524,7 +529,8 @@ export default class Service extends React.Component<ServiceProps> {
responseStatus:
result?.status === undefined ? (store.error ? 1 : 0) : result?.status,
responseMsg: store.msg
})
}),
this
);

if (!isEmpty(data) && onBulkChange) {
Expand All @@ -537,14 +543,18 @@ export default class Service extends React.Component<ServiceProps> {
afterSchemaFetch(schema: any) {
const {onBulkChange, formStore, dispatchEvent, store} = this.props;

dispatchEvent?.('fetchSchemaInited', {
...schema,
__response: {msg: store.msg, error: store.error}, // 保留,兼容历史
responseData: schema,
responseStatus:
schema?.status === undefined ? (store.error ? 1 : 0) : schema?.status,
responseMsg: store.msg
});
dispatchEvent?.(
'fetchSchemaInited',
{
...schema,
__response: {msg: store.msg, error: store.error}, // 保留,兼容历史
responseData: schema,
responseStatus:
schema?.status === undefined ? (store.error ? 1 : 0) : schema?.status,
responseMsg: store.msg
},
this
);

if (formStore && schema?.data && onBulkChange) {
onBulkChange && onBulkChange(schema.data);
Expand Down
7 changes: 6 additions & 1 deletion packages/amis/src/renderers/Wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ export default class Wizard extends React.Component<WizardProps, WizardState> {
'startStep'
];

static circularEventAction: {[eventName: string]: string} = {
inited: 'reload'
};

dom: any;
form: any;
asyncCancel: () => void;
Expand Down Expand Up @@ -382,7 +386,8 @@ export default class Wizard extends React.Component<WizardProps, WizardState> {

const rendererEvent = await dispatchEvent(
action,
value ? createObject(data, value) : data
value ? createObject(data, value) : data,
this
);

return rendererEvent?.prevented ?? false;
Expand Down