Skip to content

Commit fea6ae8

Browse files
committed
[ML] Fixing wizard validation delay (elastic#45265)
* [ML] Fixing wizard validation delay * changes based on review
1 parent 7c0e39b commit fea6ae8

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_validator/job_validator.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class JobValidator {
3737
private _jobCreator: JobCreatorType;
3838
private _validationSummary: ValidationSummary;
3939
private _lastJobConfig: string;
40-
private _validateTimeout: NodeJS.Timeout;
40+
private _validateTimeout: ReturnType<typeof setTimeout> | null = null;
4141
private _existingJobsAndGroups: ExistingJobsAndGroups;
4242
private _basicValidations: BasicValidations = {
4343
jobId: { valid: true },
@@ -46,6 +46,7 @@ export class JobValidator {
4646
bucketSpan: { valid: true },
4747
duplicateDetectors: { valid: true },
4848
};
49+
private _validating: boolean = false;
4950

5051
constructor(jobCreator: JobCreatorType, existingJobsAndGroups: ExistingJobsAndGroups) {
5152
this._jobCreator = jobCreator;
@@ -54,25 +55,30 @@ export class JobValidator {
5455
basic: false,
5556
advanced: false,
5657
};
57-
this._validateTimeout = setTimeout(() => {}, 0);
5858
this._existingJobsAndGroups = existingJobsAndGroups;
5959
}
6060

61-
public validate() {
61+
public validate(callback: () => void) {
62+
this._validating = true;
6263
const formattedJobConfig = this._jobCreator.formattedJobJson;
63-
return new Promise((resolve: () => void) => {
64-
// only validate if the config has changed
65-
if (formattedJobConfig !== this._lastJobConfig) {
64+
// only validate if the config has changed
65+
if (formattedJobConfig !== this._lastJobConfig) {
66+
if (this._validateTimeout !== null) {
67+
// clear any previous on going validation timeouts
6668
clearTimeout(this._validateTimeout);
67-
this._lastJobConfig = formattedJobConfig;
68-
this._validateTimeout = setTimeout(() => {
69-
this._runBasicValidation();
70-
resolve();
71-
}, VALIDATION_DELAY_MS);
72-
} else {
73-
resolve();
7469
}
75-
});
70+
this._lastJobConfig = formattedJobConfig;
71+
this._validateTimeout = setTimeout(() => {
72+
this._runBasicValidation();
73+
this._validating = false;
74+
this._validateTimeout = null;
75+
callback();
76+
}, VALIDATION_DELAY_MS);
77+
} else {
78+
// _validating is still true if there is a previous validation timeout on going.
79+
this._validating = this._validateTimeout !== null;
80+
}
81+
callback();
7682
}
7783

7884
private _resetBasicValidations() {
@@ -135,4 +141,8 @@ export class JobValidator {
135141
public set advancedValid(valid: boolean) {
136142
this._validationSummary.advanced = valid;
137143
}
144+
145+
public get validating(): boolean {
146+
return this._validating;
147+
}
138148
}

x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/components/job_details_step/job_details.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export const JobDetailsStep: FC<Props> = ({
3737
const active =
3838
jobValidator.jobId.valid &&
3939
jobValidator.modelMemoryLimit.valid &&
40-
jobValidator.groupIds.valid;
40+
jobValidator.groupIds.valid &&
41+
jobValidator.validating === false;
4142
setNextActive(active);
4243
}, [jobValidatorUpdated]);
4344

x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/components/pick_fields_step/pick_fields.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export const PickFieldsStep: FC<StepProps> = ({ setCurrentStep, isCurrentStep })
3030
const active =
3131
jobCreator.detectors.length > 0 &&
3232
jobValidator.bucketSpan.valid &&
33-
jobValidator.duplicateDetectors.valid;
33+
jobValidator.duplicateDetectors.valid &&
34+
jobValidator.validating === false;
3435
setNextActive(active);
3536
}, [jobValidatorUpdated]);
3637

x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/wizard.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,9 @@ export const Wizard: FC<Props> = ({
8686
);
8787

8888
useEffect(() => {
89-
// IIFE to run the validation. the useEffect callback can't be async
90-
(async () => {
91-
await jobValidator.validate();
89+
jobValidator.validate(() => {
9290
setJobValidatorUpdate(jobValidatorUpdated);
93-
})();
91+
});
9492

9593
// if the job config has changed, reset the highestStep
9694
// compare a stringified config to ensure the configs have actually changed

0 commit comments

Comments
 (0)