@@ -2,7 +2,7 @@ import { merge } from '@rc-component/util/lib/utils/set';
22import { mergeWith } from '@rc-component/util' ;
33import warning from '@rc-component/util/lib/warning' ;
44import * as React from 'react' ;
5- import { HOOK_MARK } from './FieldContext' ;
5+ import { HOOK_MARK } from '.. /FieldContext' ;
66import type {
77 Callbacks ,
88 FieldData ,
@@ -26,20 +26,19 @@ import type {
2626 ValidateErrorEntity ,
2727 ValidateMessages ,
2828 ValuedNotifyInfo ,
29- WatchCallBack ,
30- } from './interface' ;
31- import { allPromiseFinish } from './utils/asyncUtil' ;
32- import { defaultValidateMessages } from './utils/messages' ;
33- import NameMap from './utils/NameMap' ;
29+ } from '../interface' ;
30+ import { allPromiseFinish } from '../utils/asyncUtil' ;
31+ import { defaultValidateMessages } from '../utils/messages' ;
32+ import NameMap from '../utils/NameMap' ;
3433import {
3534 cloneByNamePathList ,
3635 containsNamePath ,
3736 getNamePath ,
3837 getValue ,
3938 matchNamePath ,
4039 setValue ,
41- } from './utils/valueUtil' ;
42- import type { BatchTask } from './BatchUpdate ' ;
40+ } from '.. /utils/valueUtil' ;
41+ import WatcherCenter from './useNotifyWatch ' ;
4342
4443type FlexibleFieldEntity = Partial < FieldEntity > ;
4544
@@ -78,6 +77,8 @@ export class FormStore {
7877
7978 private lastValidatePromise : Promise < FieldError [ ] > = null ;
8079
80+ private watcherCenter = new WatcherCenter ( this ) ;
81+
8182 constructor ( forceRootUpdate : ( ) => void ) {
8283 this . forceRootUpdate = forceRootUpdate ;
8384 }
@@ -121,7 +122,6 @@ export class FormStore {
121122 setPreserve : this . setPreserve ,
122123 getInitialValue : this . getInitialValue ,
123124 registerWatch : this . registerWatch ,
124- setBatchUpdate : this . setBatchUpdate ,
125125 } ;
126126 }
127127
@@ -195,47 +195,12 @@ export class FormStore {
195195 } ;
196196
197197 // ============================= Watch ============================
198- private watchList : WatchCallBack [ ] = [ ] ;
199-
200198 private registerWatch : InternalHooks [ 'registerWatch' ] = callback => {
201- this . watchList . push ( callback ) ;
202-
203- return ( ) => {
204- this . watchList = this . watchList . filter ( fn => fn !== callback ) ;
205- } ;
199+ return this . watcherCenter . register ( callback ) ;
206200 } ;
207201
208202 private notifyWatch = ( namePath : InternalNamePath [ ] = [ ] ) => {
209- // No need to cost perf when nothing need to watch
210- if ( this . watchList . length ) {
211- const values = this . getFieldsValue ( ) ;
212- const allValues = this . getFieldsValue ( true ) ;
213-
214- this . watchList . forEach ( callback => {
215- callback ( values , allValues , namePath ) ;
216- } ) ;
217- }
218- } ;
219-
220- private notifyWatchNamePathList : InternalNamePath [ ] = [ ] ;
221- private batchNotifyWatch = ( namePath : InternalNamePath ) => {
222- this . notifyWatchNamePathList . push ( namePath ) ;
223- this . batch ( 'notifyWatch' , ( ) => {
224- this . notifyWatch ( this . notifyWatchNamePathList ) ;
225- this . notifyWatchNamePathList = [ ] ;
226- } ) ;
227- } ;
228-
229- // ============================= Batch ============================
230- private batchUpdate : BatchTask ;
231-
232- private setBatchUpdate = ( batchUpdate : BatchTask ) => {
233- this . batchUpdate = batchUpdate ;
234- } ;
235-
236- // Batch call the task, only last will be called
237- private batch = ( key : string , callback : VoidFunction ) => {
238- this . batchUpdate ( key , callback ) ;
203+ this . watcherCenter . notify ( namePath ) ;
239204 } ;
240205
241206 // ========================== Dev Warning =========================
@@ -669,7 +634,7 @@ export class FormStore {
669634 private registerField = ( entity : FieldEntity ) => {
670635 this . fieldEntities . push ( entity ) ;
671636 const namePath = entity . getNamePath ( ) ;
672- this . batchNotifyWatch ( namePath ) ;
637+ this . notifyWatch ( [ namePath ] ) ;
673638
674639 // Set initial values
675640 if ( entity . props . initialValue !== undefined ) {
@@ -709,7 +674,7 @@ export class FormStore {
709674 }
710675 }
711676
712- this . batchNotifyWatch ( namePath ) ;
677+ this . notifyWatch ( [ namePath ] ) ;
713678 } ;
714679 } ;
715680
@@ -1078,6 +1043,7 @@ function useForm<Values = any>(form?: FormInstance<Values>): [FormInstance<Value
10781043 const formRef = React . useRef < FormInstance > ( null ) ;
10791044 const [ , forceUpdate ] = React . useState ( { } ) ;
10801045
1046+ // Create singleton FormStore
10811047 if ( ! formRef . current ) {
10821048 if ( form ) {
10831049 formRef . current = form ;
0 commit comments