@@ -21,6 +21,7 @@ import type {
2121 InternalFieldData ,
2222 ValuedNotifyInfo ,
2323 RuleError ,
24+ WatchCallBack ,
2425} from './interface' ;
2526import { HOOK_MARK } from './FieldContext' ;
2627import { allPromiseFinish } from './utils/asyncUtil' ;
@@ -93,6 +94,7 @@ export class FormStore {
9394 setFieldsValue : this . setFieldsValue ,
9495 validateFields : this . validateFields ,
9596 submit : this . submit ,
97+ _init : true ,
9698
9799 getInternalHooks : this . getInternalHooks ,
98100 } ) ;
@@ -114,6 +116,7 @@ export class FormStore {
114116 getFields : this . getFields ,
115117 setPreserve : this . setPreserve ,
116118 getInitialValue : this . getInitialValue ,
119+ registerWatch : this . registerWatch ,
117120 } ;
118121 }
119122
@@ -141,6 +144,7 @@ export class FormStore {
141144
142145 // We will take consider prev form unmount fields.
143146 // When the field is not `preserve`, we need fill this with initialValues instead of store.
147+ // eslint-disable-next-line array-callback-return
144148 this . prevWithoutPreserves ?. map ( ( { key : namePath } ) => {
145149 nextStore = setValue ( nextStore , namePath , getValue ( initialValues , namePath ) ) ;
146150 } ) ;
@@ -180,6 +184,28 @@ export class FormStore {
180184 this . preserve = preserve ;
181185 } ;
182186
187+ // ============================= Watch ============================
188+ private watchList : WatchCallBack [ ] = [ ] ;
189+
190+ private registerWatch : InternalHooks [ 'registerWatch' ] = callback => {
191+ this . watchList . push ( callback ) ;
192+
193+ return ( ) => {
194+ this . watchList = this . watchList . filter ( fn => fn !== callback ) ;
195+ } ;
196+ } ;
197+
198+ private notifyWatch = ( namePath : InternalNamePath [ ] = [ ] ) => {
199+ // No need to cost perf when nothing need to watch
200+ if ( this . watchList . length ) {
201+ const values = this . getFieldsValue ( ) ;
202+
203+ this . watchList . forEach ( callback => {
204+ callback ( values , namePath ) ;
205+ } ) ;
206+ }
207+ } ;
208+
183209 // ========================== Dev Warning =========================
184210 private timeoutId : any = null ;
185211
@@ -498,6 +524,7 @@ export class FormStore {
498524 this . updateStore ( setValues ( { } , this . initialValues ) ) ;
499525 this . resetWithFieldInitialValue ( ) ;
500526 this . notifyObservers ( prevStore , null , { type : 'reset' } ) ;
527+ this . notifyWatch ( ) ;
501528 return ;
502529 }
503530
@@ -509,16 +536,20 @@ export class FormStore {
509536 } ) ;
510537 this . resetWithFieldInitialValue ( { namePathList } ) ;
511538 this . notifyObservers ( prevStore , namePathList , { type : 'reset' } ) ;
539+ this . notifyWatch ( namePathList ) ;
512540 } ;
513541
514542 private setFields = ( fields : FieldData [ ] ) => {
515543 this . warningUnhooked ( ) ;
516544
517545 const prevStore = this . store ;
518546
547+ const namePathList : InternalNamePath [ ] = [ ] ;
548+
519549 fields . forEach ( ( fieldData : FieldData ) => {
520550 const { name, errors, ...data } = fieldData ;
521551 const namePath = getNamePath ( name ) ;
552+ namePathList . push ( namePath ) ;
522553
523554 // Value
524555 if ( 'value' in data ) {
@@ -530,6 +561,8 @@ export class FormStore {
530561 data : fieldData ,
531562 } ) ;
532563 } ) ;
564+
565+ this . notifyWatch ( namePathList ) ;
533566 } ;
534567
535568 private getFields = ( ) : InternalFieldData [ ] => {
@@ -573,6 +606,8 @@ export class FormStore {
573606
574607 private registerField = ( entity : FieldEntity ) => {
575608 this . fieldEntities . push ( entity ) ;
609+ const namePath = entity . getNamePath ( ) ;
610+ this . notifyWatch ( [ namePath ] ) ;
576611
577612 // Set initial values
578613 if ( entity . props . initialValue !== undefined ) {
@@ -591,8 +626,6 @@ export class FormStore {
591626 const mergedPreserve = preserve !== undefined ? preserve : this . preserve ;
592627
593628 if ( mergedPreserve === false && ( ! isListField || subNamePath . length > 1 ) ) {
594- const namePath = entity . getNamePath ( ) ;
595-
596629 const defaultValue = isListField ? undefined : this . getInitialValue ( namePath ) ;
597630
598631 if (
@@ -614,6 +647,8 @@ export class FormStore {
614647 this . triggerDependenciesUpdate ( prevStore , namePath ) ;
615648 }
616649 }
650+
651+ this . notifyWatch ( [ namePath ] ) ;
617652 } ;
618653 } ;
619654
@@ -679,6 +714,7 @@ export class FormStore {
679714 type : 'valueUpdate' ,
680715 source : 'internal' ,
681716 } ) ;
717+ this . notifyWatch ( [ namePath ] ) ;
682718
683719 // Dependencies update
684720 const childrenFields = this . triggerDependenciesUpdate ( prevStore , namePath ) ;
@@ -701,13 +737,15 @@ export class FormStore {
701737 const prevStore = this . store ;
702738
703739 if ( store ) {
704- this . updateStore ( setValues ( this . store , store ) ) ;
740+ const nextStore = setValues ( this . store , store ) ;
741+ this . updateStore ( nextStore ) ;
705742 }
706743
707744 this . notifyObservers ( prevStore , null , {
708745 type : 'valueUpdate' ,
709746 source : 'external' ,
710747 } ) ;
748+ this . notifyWatch ( ) ;
711749 } ;
712750
713751 private getDependencyChildrenFields = ( rootNamePath : InternalNamePath ) : InternalNamePath [ ] => {
0 commit comments