From 266a4ccb08aa09ba398b4dca508ddb1afca9f496 Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Tue, 24 Oct 2023 15:52:00 +0800 Subject: [PATCH] fix: linkage rule memory overflow (#2899) * fix: linkage rule memory overflow * fix: linkage rule memory overflow * refactor: linkage rule * refactor: linkage rule --- .../association-field/AssociationSelect.tsx | 20 ++++++------ .../schema-component/antd/form-v2/Form.tsx | 28 ++++++++++------ .../schema-component/antd/form-v2/utils.tsx | 32 ++++++++++++++----- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx index 8a57092e4..d97737223 100644 --- a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx @@ -61,17 +61,15 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => { useEffect(() => { const id = uid(); form.addEffects(id, () => { - linkageFields?.forEach((v) => { - if (v) { - //支持深层次子表单 - onFieldChange('*', (fieldPath: any) => { - if (fieldPath.props.name === v && field.value) { - props.onChange(null); - setInnerValue(null); - } - }); - } - }); + if (linkageFields?.length > 0) { + //支持深层次子表单 + onFieldChange('*', (fieldPath: any) => { + if (linkageFields.includes(fieldPath.props.name) && field.value) { + props.onChange(null); + setInnerValue(null); + } + }); + } }); return () => { diff --git a/packages/core/client/src/schema-component/antd/form-v2/Form.tsx b/packages/core/client/src/schema-component/antd/form-v2/Form.tsx index 35f048005..91f7f5d7c 100644 --- a/packages/core/client/src/schema-component/antd/form-v2/Form.tsx +++ b/packages/core/client/src/schema-component/antd/form-v2/Form.tsx @@ -1,6 +1,14 @@ import { css } from '@emotion/css'; import { FormLayout } from '@formily/antd-v5'; -import { createForm, Field, Form as FormilyForm, onFieldChange, onFieldInit, onFormInputChange } from '@formily/core'; +import { + createForm, + Field, + Form as FormilyForm, + onFieldChange, + onFieldInit, + onFieldReact, + onFormInputChange, +} from '@formily/core'; import { FieldContext, FormContext, observer, RecursionField, useField, useFieldSchema } from '@formily/react'; import { autorun } from '@formily/reactive'; import { uid } from '@formily/shared'; @@ -111,12 +119,14 @@ const WithForm = (props: WithFormProps) => { if (h.targetFields?.length) { const fields = h.targetFields.join(','); onFieldInit(`*(${fields})`, (field: any, form) => { - field['initProperty'] = { - display: field.display, - required: field.required, - pattern: field.pattern, - value: field.value || field.initialValue, - }; + setTimeout(() => { + field['initProperty'] = field?.['initProperty'] ?? { + display: field.display, + required: field.required, + pattern: field.pattern, + value: field.value || field.initialValue, + }; + }); }); onFieldChange(`*(${fields})`, ['value', 'required', 'pattern', 'display'], (field: any) => { field.linkageProperty = { @@ -127,9 +137,9 @@ const WithForm = (props: WithFormProps) => { // `onFieldReact` 有问题,没有办法被取消监听,所以这里用 `onFieldInit` 代替 onFieldInit(`*(${fields})`, (field: any, form) => { disposes.push( - autorun(() => { + autorun(async () => { linkagefields.push(field); - linkageMergeAction({ + await linkageMergeAction({ operator: h.operator, value: h.value, field, diff --git a/packages/core/client/src/schema-component/antd/form-v2/utils.tsx b/packages/core/client/src/schema-component/antd/form-v2/utils.tsx index c7b5e79aa..fe6c2d695 100644 --- a/packages/core/client/src/schema-component/antd/form-v2/utils.tsx +++ b/packages/core/client/src/schema-component/antd/form-v2/utils.tsx @@ -43,8 +43,12 @@ export const linkageMergeAction = async ({ ...field.linkageProperty, required: requiredResult, }; - field.required = last(field.linkageProperty?.required); - break; + return new Promise((resolve) => { + setTimeout(() => { + field.required = last(field.linkageProperty.required); + }); + resolve(void 0); + }); case ActionType.InRequired: if (await conditionAnalyses({ rules: condition, formValues: values, variables, localVariables })) { requiredResult.push(false); @@ -53,8 +57,12 @@ export const linkageMergeAction = async ({ ...field.linkageProperty, required: requiredResult, }; - field.required = last(field.linkageProperty?.required); - break; + return new Promise((resolve) => { + setTimeout(() => { + field.required = last(field.linkageProperty.required); + }); + resolve(void 0); + }); case ActionType.Visible: case ActionType.None: case ActionType.Hidden: @@ -65,8 +73,12 @@ export const linkageMergeAction = async ({ ...field.linkageProperty, display: displayResult, }; - field.display = last(field.linkageProperty?.display); - break; + return new Promise((resolve) => { + setTimeout(() => { + field.display = last(displayResult); + }); + resolve(void 0); + }); case ActionType.Editable: case ActionType.ReadOnly: case ActionType.ReadPretty: @@ -77,8 +89,12 @@ export const linkageMergeAction = async ({ ...field.linkageProperty, pattern: patternResult, }; - field.pattern = last(field.linkageProperty.pattern); - break; + return new Promise((resolve) => { + setTimeout(() => { + field.pattern = last(patternResult); + }); + resolve(void 0); + }); case ActionType.Value: if ( isConditionEmpty(condition) ||