Skip to content

Commit 3ea6416

Browse files
authored
perf(core): improve validate perf (#755)
* perf(core): improve validate perf * fix(project): fix ci * fix(core): fix null value
1 parent 7b0d3a8 commit 3ea6416

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

packages/core/src/index.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,16 @@ export function createForm<FieldProps, VirtualFieldProps>(
260260
const isEmptyInitialValue = !isValid(published.initialValue)
261261
if (isEmptyValue || isEmptyInitialValue) {
262262
field.setSourceState((state: IFieldState<FieldProps>) => {
263-
if (isEmptyValue) state.value = getFormValuesIn(state.name)
264-
if (isEmptyInitialValue)
265-
state.initialValue = getFormInitialValuesIn(state.name)
263+
if (isEmptyValue) {
264+
const formValue = getFormValuesIn(state.name)
265+
state.value = isValid(formValue) ? formValue : state.value
266+
}
267+
if (isEmptyInitialValue) {
268+
const formInitialValue = getFormInitialValuesIn(state.name)
269+
state.initialValue = isValid(formInitialValue)
270+
? formInitialValue
271+
: state.initialValue
272+
}
266273
})
267274
}
268275
}
@@ -472,6 +479,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
472479
computeState,
473480
dataType,
474481
useDirty,
482+
unmountRemoveValue,
475483
props
476484
}: Exclude<IFieldStateProps, 'dataPath' | 'nodePath'>): IField {
477485
let field: IField
@@ -486,6 +494,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
486494
dataPath,
487495
computeState,
488496
dataType,
497+
unmountRemoveValue,
489498
useDirty: isValid(useDirty) ? useDirty : options.useDirty
490499
})
491500
field.subscription = {
@@ -508,7 +517,9 @@ export function createForm<FieldProps, VirtualFieldProps>(
508517
// initialValue > formInitialValue
509518
state.initialValue = isValid(initialValue)
510519
? initialValue
511-
: formInitialValue
520+
: isValid(formInitialValue)
521+
? formInitialValue
522+
: initialValue
512523
if (isValid(visible)) {
513524
state.visible = visible
514525
}
@@ -1014,7 +1025,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
10141025
env.submittingTask = async () => {
10151026
// 增加onFormSubmitValidateStart来明确submit引起的校验开始了
10161027
heart.publish(LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_START, state)
1017-
await validate('', { throwErrors: false })
1028+
await validate('', { throwErrors: false, hostRendering: true })
10181029
const validated: IFormValidateResult = state.getState(state => ({
10191030
errors: state.errors,
10201031
warnings: state.warnings
@@ -1074,7 +1085,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
10741085
path?: FormPathPattern,
10751086
opts?: IFormExtendedValidateFieldOptions
10761087
): Promise<IFormValidateResult> {
1077-
const { throwErrors = true } = opts || {}
1088+
const { throwErrors = true, hostRendering } = opts || {}
10781089
if (!state.getState(state => state.validating)) {
10791090
state.setSourceState(state => {
10801091
state.validating = true
@@ -1087,14 +1098,14 @@ export function createForm<FieldProps, VirtualFieldProps>(
10871098
}
10881099

10891100
heart.publish(LifeCycleTypes.ON_FORM_VALIDATE_START, state)
1090-
if (graph.size > 100) env.hostRendering = true
1101+
if (graph.size > 100 && hostRendering) env.hostRendering = true
10911102
const payload = await validator.validate(path, opts)
10921103
clearTimeout(env.validateTimer)
10931104
state.setState(state => {
10941105
state.validating = false
10951106
})
10961107
heart.publish(LifeCycleTypes.ON_FORM_VALIDATE_END, state)
1097-
if (graph.size > 100) {
1108+
if (graph.size > 100 && hostRendering) {
10981109
heart.publish(LifeCycleTypes.ON_FORM_HOST_RENDER, state)
10991110
env.hostRendering = false
11001111
}

scripts/build.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function diagnositcReporter(diagnostic: ts.Diagnostic) {
1515
console.error(msg);
1616
}
1717

18-
export function getCompileConfig(configPath: string, extendOptions?: ts.CompilerOptions) {
18+
export function getCompileConfig(configPath: string, extendOptions?: any) {
1919
const host: ts.ParseConfigFileHost = ts.sys as any
2020
host.onUnRecoverableConfigFileDiagnostic = diagnositcReporter
2121
const parsedCmd = ts.getParsedCommandLineOfConfigFile(configPath, extendOptions, host);
@@ -28,7 +28,7 @@ export function getCompileConfig(configPath: string, extendOptions?: ts.Compiler
2828
}
2929

3030

31-
export function compile(rootNames: string[], options: ts.CompilerOptions, customTransformers?: ts.CustomTransformers) {
31+
export function compile(rootNames: string[], options: any, customTransformers?: ts.CustomTransformers) {
3232
const program = ts.createProgram({ rootNames, options })
3333
const emitResult = program.emit(
3434
undefined,

0 commit comments

Comments
 (0)