Skip to content

Commit

Permalink
perf(core): improve validate perf (#755)
Browse files Browse the repository at this point in the history
* perf(core): improve validate perf

* fix(project): fix ci

* fix(core): fix null value
  • Loading branch information
janryWang authored Mar 28, 2020
1 parent 7b0d3a8 commit 3ea6416
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
27 changes: 19 additions & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,16 @@ export function createForm<FieldProps, VirtualFieldProps>(
const isEmptyInitialValue = !isValid(published.initialValue)
if (isEmptyValue || isEmptyInitialValue) {
field.setSourceState((state: IFieldState<FieldProps>) => {
if (isEmptyValue) state.value = getFormValuesIn(state.name)
if (isEmptyInitialValue)
state.initialValue = getFormInitialValuesIn(state.name)
if (isEmptyValue) {
const formValue = getFormValuesIn(state.name)
state.value = isValid(formValue) ? formValue : state.value
}
if (isEmptyInitialValue) {
const formInitialValue = getFormInitialValuesIn(state.name)
state.initialValue = isValid(formInitialValue)
? formInitialValue
: state.initialValue
}
})
}
}
Expand Down Expand Up @@ -472,6 +479,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
computeState,
dataType,
useDirty,
unmountRemoveValue,
props
}: Exclude<IFieldStateProps, 'dataPath' | 'nodePath'>): IField {
let field: IField
Expand All @@ -486,6 +494,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
dataPath,
computeState,
dataType,
unmountRemoveValue,
useDirty: isValid(useDirty) ? useDirty : options.useDirty
})
field.subscription = {
Expand All @@ -508,7 +517,9 @@ export function createForm<FieldProps, VirtualFieldProps>(
// initialValue > formInitialValue
state.initialValue = isValid(initialValue)
? initialValue
: formInitialValue
: isValid(formInitialValue)
? formInitialValue
: initialValue
if (isValid(visible)) {
state.visible = visible
}
Expand Down Expand Up @@ -1014,7 +1025,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
env.submittingTask = async () => {
// 增加onFormSubmitValidateStart来明确submit引起的校验开始了
heart.publish(LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_START, state)
await validate('', { throwErrors: false })
await validate('', { throwErrors: false, hostRendering: true })
const validated: IFormValidateResult = state.getState(state => ({
errors: state.errors,
warnings: state.warnings
Expand Down Expand Up @@ -1074,7 +1085,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
path?: FormPathPattern,
opts?: IFormExtendedValidateFieldOptions
): Promise<IFormValidateResult> {
const { throwErrors = true } = opts || {}
const { throwErrors = true, hostRendering } = opts || {}
if (!state.getState(state => state.validating)) {
state.setSourceState(state => {
state.validating = true
Expand All @@ -1087,14 +1098,14 @@ export function createForm<FieldProps, VirtualFieldProps>(
}

heart.publish(LifeCycleTypes.ON_FORM_VALIDATE_START, state)
if (graph.size > 100) env.hostRendering = true
if (graph.size > 100 && hostRendering) env.hostRendering = true
const payload = await validator.validate(path, opts)
clearTimeout(env.validateTimer)
state.setState(state => {
state.validating = false
})
heart.publish(LifeCycleTypes.ON_FORM_VALIDATE_END, state)
if (graph.size > 100) {
if (graph.size > 100 && hostRendering) {
heart.publish(LifeCycleTypes.ON_FORM_HOST_RENDER, state)
env.hostRendering = false
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function diagnositcReporter(diagnostic: ts.Diagnostic) {
console.error(msg);
}

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


export function compile(rootNames: string[], options: ts.CompilerOptions, customTransformers?: ts.CustomTransformers) {
export function compile(rootNames: string[], options: any, customTransformers?: ts.CustomTransformers) {
const program = ts.createProgram({ rootNames, options })
const emitResult = program.emit(
undefined,
Expand Down

0 comments on commit 3ea6416

Please sign in to comment.