Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Aug 30, 2024
1 parent f9878c9 commit bfd5de6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/.vitepress/theme/components/createStore.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 7 additions & 8 deletions packages/r-store/src/shared/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Controller<T = any> {

readonly _list: Set<Controller>;

_safeGetState: () => T;
_getStateSafe: () => T;

_effect: ReactiveEffect<T>;

Expand All @@ -76,24 +76,24 @@ export class Controller<T = any> {
_isActive = true;

constructor(
readonly _state: () => T,
readonly _getState: () => T,
readonly _lifeCycle: LifeCycle,
_list: Set<Controller>,
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`);
}
return;
}

this.run();

if (this._lifeCycle.canUpdateComponent) {
if (this._lifeCycle.syncUpdateComponent) {
this.notify();
Expand Down Expand Up @@ -150,7 +150,7 @@ export class Controller<T = any> {
};

getSelectorState = () => {
return this._safeGetState();
return this._getStateSafe();
};

getLifeCycle = () => {
Expand All @@ -159,7 +159,6 @@ export class Controller<T = any> {

// TODO move into constructor function?
run() {
if (!this._isActive) return;
this._effect.run();
}

Expand Down
1 change: 1 addition & 0 deletions packages/r-store/src/shared/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const createHook = <T extends Record<string, unknown>, C extends Record<s
}
});

// may not work will with hmr
const prevSelector = currentIsStable ? selector : usePrevValue(selector);

const ControllerInstance = useMemo(() => new Controller(() => selectorRef(reactiveState as any), lifeCycle, controllerList, namespace, getSelected), []);
Expand Down

0 comments on commit bfd5de6

Please sign in to comment.