1- import { observable , computed , action , reaction , autorun , runInAction , when } from 'mobx'
1+ import { observable , computed , action , reaction , autorun , when , makeObservable } from 'mobx'
22import { ComposibleValidatable , Validator , Validated , ValidationResponse , ValidateStatus , Error , ValidateResult } from './types'
33import { applyValidators , debounce , isPromiseLike } from './utils'
44import Disposable from './disposable'
@@ -137,15 +137,15 @@ export default class FieldState<TValue> extends Disposable implements Composible
137137 return
138138 }
139139
140- runInAction ( 'set-validateStatus-when-_validate' , ( ) => {
140+ action ( 'set-validateStatus-when-_validate' , ( ) => {
141141 this . _validateStatus = ValidateStatus . Validating
142- } )
142+ } ) ( )
143143
144144 const response = applyValidators ( value , this . _validators )
145145
146- runInAction ( 'set-validation-when-_validate' , ( ) => {
146+ action ( 'set-validation-when-_validate' , ( ) => {
147147 this . validation = { value, response }
148- } )
148+ } ) ( )
149149 }
150150
151151 /**
@@ -154,11 +154,11 @@ export default class FieldState<TValue> extends Disposable implements Composible
154154 async validate ( ) : Promise < ValidateResult < TValue > > {
155155 const validation = this . validation
156156
157- runInAction ( 'activate-and-sync-_value-when-validate' , ( ) => {
157+ action ( 'activate-and-sync-_value-when-validate' , ( ) => {
158158 this . _activated = true
159159 // 若有用户交互产生的变更(因 debounce)尚未同步,同步之,确保本次 validate 结果是相对稳定的
160160 this . value = this . _value
161- } )
161+ } ) ( )
162162
163163 // 若 `validation` 未发生变更,意味着未发生新的校验行为
164164 // 若上边操作未触发自动的校验行为,强制调用之
@@ -219,19 +219,21 @@ export default class FieldState<TValue> extends Disposable implements Composible
219219 return
220220 }
221221
222- runInAction ( 'endValidation' , ( ) => {
222+ action ( 'endValidation' , ( ) => {
223223 this . validation = undefined
224224 this . _validateStatus = ValidateStatus . Validated
225225
226226 if ( error !== this . error ) {
227227 this . setError ( error )
228228 }
229- } )
229+ } ) ( )
230230 }
231231
232232 constructor ( private initialValue : TValue , delay = 200 ) {
233233 super ( )
234234
235+ makeObservable ( this )
236+
235237 this . reset ( )
236238
237239 // debounced reaction to `_value` change
@@ -242,11 +244,11 @@ export default class FieldState<TValue> extends Disposable implements Composible
242244 // see https://github.com/mobxjs/mobx/issues/1956
243245 debounce ( ( ) => {
244246 if ( this . value !== this . _value ) {
245- runInAction ( 'sync-value-when-_value-changed' , ( ) => {
247+ action ( 'sync-value-when-_value-changed' , ( ) => {
246248 this . value = this . _value
247249 this . _validateStatus = ValidateStatus . NotValidated
248250 this . _activated = true
249- } )
251+ } ) ( )
250252 }
251253 } , delay ) ,
252254 { name : 'reaction-when-_value-change' }
0 commit comments