Skip to content

Commit

Permalink
fix strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Oct 12, 2024
1 parent fb19109 commit 451f0e8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
5 changes: 3 additions & 2 deletions app/.vitepress/theme/components/example.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { createElement } from 'react';
import { useData } from 'vitepress'
import { StrictMode } from 'react';
import { Fragment } from 'react';
const styleTag = ref<HTMLStyleElement>()
Expand All @@ -38,15 +39,15 @@ onMounted(() => {
const App = () => {
const { count, add } = useCount(s => s);
return createElement(StrictMode, null,
return createElement(Fragment, null,
createElement('p', { className: 'text' }, 'Reactive count component: '),
createElement('button', {
className: 'button',
onClick: add
}, `Counter is: ${count}`))
}
app = hydrateRoot(wrapperRef.value!, createElement(App));
app = hydrateRoot(wrapperRef.value!, createElement(StrictMode, null, createElement(App)));
})
onMounted(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/r-store/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactivity-store",
"version": "0.3.5",
"version": "0.3.6",
"author": "MrWangJustToDo",
"license": "MIT",
"description": "a reactive store, make you write reactive logic in react app just like zustand",
Expand Down
17 changes: 11 additions & 6 deletions packages/r-store/src/shared/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ export class Controller<T = any> {
this._effect = new ControllerEffect(this._getStateSafe, () => {
this.run();

if (!this._isActive) {
if (__DEV__) {
console.error(`[reactivity-store] unexpected update for reactivity-store, current store have been inactivated`);
}
return;
}
if (!this._isActive) return;

if (this._lifeCycle.canUpdateComponent) {
if (this._lifeCycle.syncUpdateComponent) {
Expand All @@ -125,6 +120,8 @@ export class Controller<T = any> {
}

notify = () => {
if (!this._isActive) return;

// TODO implement server side initialState
if (__DEV__ && isServer) {
console.error(`[reactivity-store] unexpected update for reactivity-store, should not update a state on the server`);
Expand Down Expand Up @@ -181,4 +178,12 @@ export class Controller<T = any> {

this._isActive = false;
}

inactive() {
this._isActive = false;
}

active() {
this._isActive = true;
}
}
29 changes: 11 additions & 18 deletions packages/r-store/src/shared/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { traverse, traverseShallow } from "./tools";
import type { LifeCycle } from "./lifeCycle";
import type { DeepReadonly, UnwrapNestedRefs } from "@vue/reactivity";

const temp = new Set<Controller>();

/**
* @internal
*/
Expand Down Expand Up @@ -125,15 +123,14 @@ export const createHook = <T extends Record<string, unknown>, C extends Record<s

// initial
useMemo(() => {
if (!active) return;
ControllerInstance.run();
getSelected();
}, [ControllerInstance, getSelected]);

// !TODO try to improve the performance
// rerun when the 'selector' change
useMemo(() => {
if (active && prevSelector !== selector) {
if (prevSelector !== selector) {
ControllerInstance.run();
getSelected();
}
Expand All @@ -154,19 +151,21 @@ export const createHook = <T extends Record<string, unknown>, C extends Record<s

ControllerInstance._devResult = ref.current;

if (!active) {
console.error("current `useSelector` have been inactivated, check your code first");
}

useEffect(() => {
setDevController(ControllerInstance, initialState);

return () => {
delDevController(ControllerInstance, initialState);
};
}, []);
}, [ControllerInstance]);
}

useEffect(() => () => ControllerInstance.stop(), [ControllerInstance]);
useEffect(() => {
ControllerInstance.active();
return () => {
ControllerInstance.inactive();
};
}, [ControllerInstance]);

return ref.current;
};
Expand Down Expand Up @@ -239,17 +238,11 @@ export const createHook = <T extends Record<string, unknown>, C extends Record<s
}
};

const controller = new Controller(subscribeSelector, lifeCycle, temp, InternalNameSpace.$$__subscribe__$$, () => cb());
const controller = new Controller(subscribeSelector, lifeCycle, controllerList, InternalNameSpace.$$__subscribe__$$, () => cb());

if (active) {
controller.run();
}
controller.run();

if (__DEV__) {
if (!active) {
console.error("can not subscribe an inactivated hook, check your code first");
}

setDevController(controller, initialState);
}

Expand Down

0 comments on commit 451f0e8

Please sign in to comment.