From bfd5de6e759ab691379338a7a09c54b51ddceb89 Mon Sep 17 00:00:00 2001 From: MrWangJustToDo <2711470541@qq.com> Date: Fri, 30 Aug 2024 10:41:39 +0800 Subject: [PATCH] improve --- app/.vitepress/theme/components/createStore.vue | 2 +- packages/r-store/src/shared/controller.ts | 15 +++++++-------- packages/r-store/src/shared/hook.ts | 1 + 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/.vitepress/theme/components/createStore.vue b/app/.vitepress/theme/components/createStore.vue index f6988f3..b6e4c01 100644 --- a/app/.vitepress/theme/components/createStore.vue +++ b/app/.vitepress/theme/components/createStore.vue @@ -18,7 +18,7 @@ const useCount = createStore(() => { }); const App = () => { - const { reactiveCount, changeCount, refCount } = useCount((state) => state); + const { reactiveCount, changeCount, refCount } = useCount.useDeepStableSelector((state) => state); return React.createElement( "div", diff --git a/packages/r-store/src/shared/controller.ts b/packages/r-store/src/shared/controller.ts index 034b9ba..dc105e6 100644 --- a/packages/r-store/src/shared/controller.ts +++ b/packages/r-store/src/shared/controller.ts @@ -50,7 +50,7 @@ export class Controller { readonly _list: Set; - _safeGetState: () => T; + _getStateSafe: () => T; _effect: ReactiveEffect; @@ -76,15 +76,17 @@ export class Controller { _isActive = true; constructor( - readonly _state: () => T, + readonly _getState: () => T, readonly _lifeCycle: LifeCycle, _list: Set, readonly _namespace?: string, readonly _onUpdate?: () => void ) { - this._safeGetState = catchError(_state, this); + this._getStateSafe = catchError(_getState, this); + + this._effect = new ControllerEffect(this._getStateSafe, () => { + this.run(); - this._effect = new ControllerEffect(this._safeGetState, () => { if (!this._isActive) { if (__DEV__) { console.error(`[reactivity-store] unexpected update for reactivity-store, current store have been inactivated`); @@ -92,8 +94,6 @@ export class Controller { return; } - this.run(); - if (this._lifeCycle.canUpdateComponent) { if (this._lifeCycle.syncUpdateComponent) { this.notify(); @@ -150,7 +150,7 @@ export class Controller { }; getSelectorState = () => { - return this._safeGetState(); + return this._getStateSafe(); }; getLifeCycle = () => { @@ -159,7 +159,6 @@ export class Controller { // TODO move into constructor function? run() { - if (!this._isActive) return; this._effect.run(); } diff --git a/packages/r-store/src/shared/hook.ts b/packages/r-store/src/shared/hook.ts index 494e995..f9c99c1 100644 --- a/packages/r-store/src/shared/hook.ts +++ b/packages/r-store/src/shared/hook.ts @@ -115,6 +115,7 @@ export const createHook = , C extends Record new Controller(() => selectorRef(reactiveState as any), lifeCycle, controllerList, namespace, getSelected), []);