Skip to content

Commit a7e3373

Browse files
authored
chore: Opt of form store (#143)
* unique fetch store * downgress to string path match * trigger warning when not have a checker * revert NameMap
1 parent 6b7cd4e commit a7e3373

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

src/Field.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,10 @@ class Field extends React.Component<InternalFieldProps, FieldState, InternalForm
186186
// Trigger by store update. Check if need update the component
187187
public onStoreChange: FieldEntity['onStoreChange'] = (prevStore, namePathList, info) => {
188188
const { shouldUpdate, dependencies = [], onReset } = this.props;
189-
const { getFieldsValue }: FormInstance = this.context;
190-
const values = getFieldsValue(true);
189+
const { store } = info;
191190
const namePath = this.getNamePath();
192191
const prevValue = this.getValue(prevStore);
193-
const curValue = this.getValue();
192+
const curValue = this.getValue(store);
194193

195194
const namePathMatch = namePathList && containsNamePath(namePathList, namePath);
196195

@@ -242,7 +241,7 @@ class Field extends React.Component<InternalFieldProps, FieldState, InternalForm
242241
if (
243242
shouldUpdate &&
244243
!namePath.length &&
245-
requireUpdate(shouldUpdate, prevStore, values, prevValue, curValue, info)
244+
requireUpdate(shouldUpdate, prevStore, store, prevValue, curValue, info)
246245
) {
247246
this.reRender();
248247
return;
@@ -277,7 +276,7 @@ class Field extends React.Component<InternalFieldProps, FieldState, InternalForm
277276
dependencies.some(dependency =>
278277
containsNamePath(namePathList, getNamePath(dependency)),
279278
) ||
280-
requireUpdate(shouldUpdate, prevStore, values, prevValue, curValue, info)
279+
requireUpdate(shouldUpdate, prevStore, store, prevValue, curValue, info)
281280
) {
282281
this.reRender();
283282
return;

src/interface.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ export interface ValidateErrorEntity {
8383
}
8484

8585
export interface FieldEntity {
86-
onStoreChange: (store: Store, namePathList: InternalNamePath[] | null, info: NotifyInfo) => void;
86+
onStoreChange: (
87+
store: Store,
88+
namePathList: InternalNamePath[] | null,
89+
info: ValuedNotifyInfo,
90+
) => void;
8791
isFieldTouched: () => boolean;
8892
isFieldDirty: () => boolean;
8993
isFieldValidating: () => boolean;
@@ -115,29 +119,45 @@ export type InternalValidateFields = (
115119
) => Promise<Store>;
116120
export type ValidateFields = (nameList?: NamePath[]) => Promise<Store>;
117121

122+
// >>>>>> Info
118123
interface ValueUpdateInfo {
119124
type: 'valueUpdate';
120125
source: 'internal' | 'external';
121126
}
122127

128+
interface ValidateFinishInfo {
129+
type: 'validateFinish';
130+
}
131+
132+
interface ResetInfo {
133+
type: 'reset';
134+
}
135+
136+
interface SetFieldInfo {
137+
type: 'setField';
138+
data: FieldData;
139+
}
140+
141+
interface DependenciesUpdateInfo {
142+
type: 'dependenciesUpdate';
143+
/**
144+
* Contains all the related `InternalNamePath[]`.
145+
* a <- b <- c : change `a`
146+
* relatedFields=[a, b, c]
147+
*/
148+
relatedFields: InternalNamePath[];
149+
}
150+
123151
export type NotifyInfo =
124152
| ValueUpdateInfo
125-
| {
126-
type: 'validateFinish' | 'reset';
127-
}
128-
| {
129-
type: 'setField';
130-
data: FieldData;
131-
}
132-
| {
133-
type: 'dependenciesUpdate';
134-
/**
135-
* Contains all the related `InternalNamePath[]`.
136-
* a <- b <- c : change `a`
137-
* relatedFields=[a, b, c]
138-
*/
139-
relatedFields: InternalNamePath[];
140-
};
153+
| ValidateFinishInfo
154+
| ResetInfo
155+
| SetFieldInfo
156+
| DependenciesUpdateInfo;
157+
158+
export type ValuedNotifyInfo = NotifyInfo & {
159+
store: Store;
160+
};
141161

142162
export interface Callbacks {
143163
onValuesChange?: (changedValues: Store, values: Store) => void;

src/useForm.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
StoreValue,
2020
Meta,
2121
InternalFieldData,
22+
ValuedNotifyInfo,
2223
} from './interface';
2324
import { HOOK_MARK } from './FieldContext';
2425
import { allPromiseFinish } from './utils/asyncUtil';
@@ -134,9 +135,14 @@ export class FormStore {
134135
this.validateMessages = validateMessages;
135136
};
136137

138+
// ========================== Dev Warning =========================
139+
private timeoutId: number = null;
140+
137141
private warningUnhooked = () => {
138-
if (process.env.NODE_ENV !== 'production') {
139-
setTimeout(() => {
142+
if (process.env.NODE_ENV !== 'production' && !this.timeoutId) {
143+
this.timeoutId = window.setTimeout(() => {
144+
this.timeoutId = null;
145+
140146
if (!this.formHooked) {
141147
warning(
142148
false,
@@ -441,8 +447,13 @@ export class FormStore {
441447
});
442448
};
443449

444-
private getFields = (): InternalFieldData[] =>
445-
this.getFieldEntities(true).map(
450+
private getFields = (): InternalFieldData[] => {
451+
console.time('getFieldEntities');
452+
const entities = this.getFieldEntities(true);
453+
console.timeEnd('getFieldEntities');
454+
455+
console.time('map fields');
456+
const fields = entities.map(
446457
(field: FieldEntity): InternalFieldData => {
447458
const namePath = field.getNamePath();
448459
const meta = field.getMeta();
@@ -459,6 +470,10 @@ export class FormStore {
459470
return fieldData;
460471
},
461472
);
473+
console.timeEnd('map fields');
474+
475+
return fields;
476+
};
462477

463478
// =========================== Observer ===========================
464479
private registerField = (entity: FieldEntity) => {
@@ -503,9 +518,15 @@ export class FormStore {
503518
info: NotifyInfo,
504519
) => {
505520
if (this.subscribable) {
521+
const mergedInfo: ValuedNotifyInfo = {
522+
...info,
523+
store: this.getFieldsValue(true),
524+
};
525+
console.time(`notify ${info.type}`);
506526
this.getFieldEntities().forEach(({ onStoreChange }) => {
507-
onStoreChange(prevStore, namePathList, info);
527+
onStoreChange(prevStore, namePathList, mergedInfo);
508528
});
529+
console.timeEnd(`notify ${info.type}`);
509530
} else {
510531
this.forceRootUpdate();
511532
}
@@ -605,7 +626,9 @@ export class FormStore {
605626
const { onFieldsChange } = this.callbacks;
606627

607628
if (onFieldsChange) {
629+
console.time('getFields');
608630
const fields = this.getFields();
631+
console.timeEnd('getFields');
609632

610633
/**
611634
* Fill errors since `fields` may be replaced by controlled fields

0 commit comments

Comments
 (0)