Skip to content

Commit a3a374d

Browse files
committed
feat: integrate multiple verifications into a transaction
1 parent fed690a commit a3a374d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

primitives/filed-form /src/form-core/createStore.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ class FormStore {
774774
});
775775
};
776776

777-
validateFields = async (names?: NamePath[], opts?: ValidateFieldsOptions) => {
777+
private validateFields = async (names?: NamePath[], opts?: ValidateFieldsOptions) => {
778778
let list: string[];
779779

780780
if (names && names.length > 0) {
@@ -807,7 +807,7 @@ class FormStore {
807807
}
808808
});
809809

810-
const results = await Promise.all(list.map(n => this.validateField(n, opts)));
810+
const results = await this.transactionAsync(() => Promise.all(list.map(n => this.validateField(n, opts))));
811811

812812
return results.every(Boolean);
813813
};
@@ -852,6 +852,18 @@ class FormStore {
852852
}
853853
};
854854

855+
private async transactionAsync<T>(fn: () => Promise<T>): Promise<T> {
856+
this.begin();
857+
try {
858+
return await fn();
859+
} catch (e) {
860+
this.rollback();
861+
throw e;
862+
} finally {
863+
this.commit();
864+
}
865+
}
866+
855867
// ===== Array Operation =====
856868
private arrayOp = (name: NamePath, op: 'insert' | 'move' | 'remove' | 'replace' | 'swap', args: any) => {
857869
const arr = this.getFieldValue(name);

0 commit comments

Comments
 (0)