11import { PATH_DELIMITER , GLOBAL_CONFIG_MANAGER } from "../consts" ;
22import { AutoStore } from "../store/store" ;
3- import { isSchemaBuilder , markRaw , setVal , withSchema } from "../utils" ;
3+ import { isRaw , isSchemaBuilder , markRaw , setVal , withSchema } from "../utils" ;
44import { getVal } from "../utils/getVal" ;
55import type { SchemaDescriptor , SchemaDescriptorBuilder , AutoStoreConfigures } from "./types" ;
66import { isFunction } from "../utils/isFunction" ;
@@ -230,6 +230,33 @@ export class ConfigManager extends AutoStore<
230230 if ( ( descriptor . schema as any ) . onInvalid === undefined ) {
231231 ( descriptor . schema as any ) . onInvalid = "throw" ;
232232 }
233+ // 安装校验器
234+ this . _installValidator ( strPath , descriptor , store ) ;
235+ // 由于该配置项可能已先load还未注册,因此需要覆盖现有的值
236+ const loadedValue = this . peep ( ( state ) =>
237+ getVal ( state , [ configKey . join ( PATH_DELIMITER ) , "value" ] ) ,
238+ ) ;
239+ // 用于为schema中的observerObject提供refStore,以便能访问
240+ this . _handleRefState ( descriptor . schema , store ) ;
241+ // 动态添加
242+ this . state [ configKey . join ( PATH_DELIMITER ) ] = descriptor . schema ;
243+ if ( loadedValue !== undefined ) {
244+ descriptor . schema . value = loadedValue ;
245+ }
246+ // 创建代理用于从原始的Store值读写状态值
247+ this . _createValueProxy ( descriptor , store , pathKey ) ;
248+
249+ // 返回初始值,避免读取代理导致循环依赖
250+ return loadedValue || initialValue ;
251+ }
252+ private _handleRefState ( schema : object , store : AutoStore < any > ) {
253+ Object . values ( schema ) . forEach ( ( v ) => {
254+ if ( isFunction ( v ) && ! isRaw ( v ) ) {
255+ v . _getRefStore = ( ) => new WeakRef ( store ) ;
256+ }
257+ } ) ;
258+ }
259+ private _installValidator ( path : string , descriptor : SchemaDescriptor , store : AutoStore < any > ) {
233260 if ( isFunction ( descriptor . schema . validate ) ) {
234261 // 将getErrorMessage 方法和validationBehavior添加到验证函数上,用于在isValidPass中使用
235262 // @ts -expect-error
@@ -252,34 +279,19 @@ export class ConfigManager extends AutoStore<
252279 if ( ! store . options . validators ) {
253280 store . options . validators = { } ;
254281 }
255- store . options . validators [ strPath ] = descriptor . schema . validate ;
282+ store . options . validators [ path ] = descriptor . schema . validate ;
256283 } else {
257284 if ( store . options . validators ) {
258- delete store . options . validators [ strPath ] ;
285+ delete store . options . validators [ path ] ;
259286 }
260287 }
261- // 由于该配置项可能已先load还未注册,因此需要覆盖现有的值
262- const loadedValue = this . peep ( ( state ) =>
263- getVal ( state , [ configKey . join ( PATH_DELIMITER ) , "value" ] ) ,
264- ) ;
265-
266- // 动态添加
267- this . state [ configKey . join ( PATH_DELIMITER ) ] = descriptor . schema ;
268- if ( loadedValue !== undefined ) {
269- descriptor . schema . value = loadedValue ;
270- }
271-
272- // 创建代理用于从原始的Store值读写状态值
273- this . _createValueProxy ( descriptor , store , pathKey ) ;
274-
275- // 返回初始值,避免读取代理导致循环依赖
276- return loadedValue || initialValue ;
277288 }
278289 private _createValueProxy (
279290 finalDescriptor : SchemaDescriptor ,
280291 store : AutoStore < any > ,
281292 path : string [ ] ,
282293 ) {
294+ // oxlint-disable-next-line typescript/no-this-alias
283295 const self = this ;
284296
285297 // 由于ConfigManager是全局对象,而Store可能是动态,可能会被销毁,因此应采用弱引用
0 commit comments