Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Aug 31, 2024
1 parent bfd5de6 commit ebec1fb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
9 changes: 6 additions & 3 deletions packages/r-store/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ export declare function createState<T extends Record<string, unknown>>(setup: Se
export declare const createStore: <T extends Record<string, unknown>>(creator: Creator<T>) => UseSelectorWithStore<T>;

/**
* @deprecated new version of React 'StrictMode' cause lifeCycle function not work as expect
* @deprecated
* new version of React 'StrictMode' cause lifeCycle function not work as expect
* try to disable `StrictMode` to fix this issue
*/
export declare function createStoreWithComponent<T extends Record<string, unknown>>(props: CreateStoreWithComponentProps<NonNullable<unknown>, T>): ({ children }: {
children?: (p: DeepReadonly<UnwrapNestedRefs<T>>) => ReactNode;
}) => ReactElement;

/**
* @deprecated new version of React 'StrictMode' cause lifeCycle function not work as expect
* @deprecated
* new version of React 'StrictMode' cause lifeCycle function not work as expect
* try to disable `StrictMode` to fix this issue
*/
export declare function createStoreWithComponent<P extends Record<string, unknown>, T extends Record<string, unknown>>(props: CreateStoreWithComponentProps<P, T>): ({ children }: {
Expand Down Expand Up @@ -293,7 +295,8 @@ export declare type WithActionsProps<T, P> = {
};

/**
* @deprecated use `withSelectorOptions` instead
* @deprecated
* use `withSelectorOptions` instead
*/
export declare const withDeepSelector: typeof withSelectorOptions;

Expand Down
10 changes: 8 additions & 2 deletions packages/r-store/src/shared/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class Controller<T = any> {

_devWithStable: any;

_devVersion: string;

_devType: any;

_devResult: any;
Expand Down Expand Up @@ -109,9 +111,13 @@ export class Controller<T = any> {
this._namespace !== InternalNameSpace.$$__redux_dev_tool__$$
) {
this._list = _list;

this._list.add(this);
}

if (__DEV__) {
this._devVersion = __VERSION__;
}
}

notify = () => {
Expand All @@ -131,7 +137,7 @@ export class Controller<T = any> {

this._lifeCycle.canUpdateComponent = false;
}

this._listeners.forEach((f) => f());
};

Expand Down
6 changes: 5 additions & 1 deletion packages/r-store/src/shared/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const sendToDevTools = (action: Action) => {
const { getUpdatedState, ...rest } = action;
try {
const state = getUpdatedState();

getDevToolInstance().send(rest, state);
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -168,6 +168,10 @@ export const connectDevTool = (
// create a subscribe controller to listen to the state change, because some state change may not trigger by the `action`
const controller = new Controller(subscribe, lifeCycle, temp, InternalNameSpace.$$__redux_dev_tool__$$, onUpdateWithoutAction);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
controller._devReduxOptions = options;

devController[name] = controller;

controller.run();
Expand Down
13 changes: 10 additions & 3 deletions packages/r-store/src/shared/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export const createHook = <T extends Record<string, unknown>, C extends Record<s

// tool function to generate `useSelector` hook
const generateUseHook = <P>(type: "default" | "deep" | "deep-stable" | "shallow" | "shallow-stable") => {
const currentIsDeep = type === 'default' ? deepSelector : type === 'deep' || type === 'deep-stable';
const currentIsDeep = type === "default" ? deepSelector : type === "deep" || type === "deep-stable";

const currentIsStable = type === 'default' ? stableSelector : type === 'deep-stable' || type === 'shallow-stable';
const currentIsStable = type === "default" ? stableSelector : type === "deep-stable" || type === "shallow-stable";

return (selector?: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P) => {
const ref = useRef<P | DeepReadonly<UnwrapNestedRefs<T>>>();
Expand Down Expand Up @@ -248,9 +248,16 @@ export const createHook = <T extends Record<string, unknown>, C extends Record<s
if (!active) {
console.error("can not subscribe an inactivated hook, check your code first");
}

setDevController(controller, initialState);
}

return () => controller.stop();
return () => {
if (__DEV__) {
delDevController(controller, initialState);
}
controller.stop();
};
};

typedUseSelector.cleanReactiveHooks = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/r-store/src/state/_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function internalCreateState<T extends Record<string, unknown>, P extends
const stableSelector = selectorOptions?.stableSelector ?? false;

if (__DEV__ && reduxDevTool) {
actions = connectDevTool(namespaceOptions.namespace, actions, rawState, reactiveState) as P;
actions = connectDevTool(namespaceOptions.namespace, actions, rawState, reactiveState, namespaceOptions) as P;
}

const useSelector = createHook<T, P & L>(reactiveState, rawState, lifeCycle, deepSelector, stableSelector, namespaceOptions.namespace, actions as P & L);
Expand Down
4 changes: 4 additions & 0 deletions packages/r-store/src/state/middleware/withPersist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export function withPersist<T extends Record<string, unknown>, P extends Record<

if (__DEV__) {
setDevController(ControllerInstance, initialState);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ControllerInstance._devPersistOptions = options;
}

return {
Expand Down

0 comments on commit ebec1fb

Please sign in to comment.