diff --git a/packages/r-store/index.d.ts b/packages/r-store/index.d.ts index f07b32c..bd6f8df 100644 --- a/packages/r-store/index.d.ts +++ b/packages/r-store/index.d.ts @@ -205,7 +205,7 @@ export declare const useReactiveState: >(initi */ export declare type UseSelectorWithState = { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; /** * @deprecated * use `getReactiveState` / `getReadonlyState` instead @@ -215,22 +215,28 @@ export declare type UseSelectorWithState = { getLifeCycle: () => LifeCycle; getReactiveState: () => UnwrapNestedRefs; getReadonlyState: () => DeepReadonly>; + /** + * + * @param selector - a method to select the state, when the state change, the `cb` will be called + * @param cb - a callback function + * @returns a unsubscribe function + */ subscribe:

(selector: (state: DeepReadonly>) => P, cb?: () => void, shallow?: boolean) => () => void; useDeepSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useDeepStableSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useShallowSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useShallowStableSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; }; @@ -239,7 +245,7 @@ export declare type UseSelectorWithState = { */ export declare type UseSelectorWithStore = { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; /** * @deprecated * use `getReactiveState` / `getReadonlyState` instead @@ -248,22 +254,28 @@ export declare type UseSelectorWithStore = { getLifeCycle: () => LifeCycle; getReactiveState: () => UnwrapNestedRefs; getReadonlyState: () => DeepReadonly>; + /** + * + * @param selector - a method to select the state, when the state change, the `cb` will be called + * @param cb - a callback function + * @returns a unsubscribe function + */ subscribe:

(selector: (state: DeepReadonly>) => P, cb?: () => void, shallow?: boolean) => () => void; useShallowSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useShallowStableSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useDeepSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useDeepStableSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; }; }; diff --git a/packages/r-store/src/__test__/type.tsx b/packages/r-store/src/__test__/type.tsx index ef5cbfe..a2272ba 100644 --- a/packages/r-store/src/__test__/type.tsx +++ b/packages/r-store/src/__test__/type.tsx @@ -43,9 +43,9 @@ const useFf = createStore(() => { return { vvv }; }); -const h = useFf((s) => s); +const h = useFf((s) => s.vvv, (p, c) => p === c); -const i = useCount((s) => s); +const i = useCount((s) => s, Object.is); const useCount_v2 = createState( withActions( diff --git a/packages/r-store/src/shared/hook.ts b/packages/r-store/src/shared/hook.ts index bdc30ab..63ee169 100644 --- a/packages/r-store/src/shared/hook.ts +++ b/packages/r-store/src/shared/hook.ts @@ -201,8 +201,8 @@ export const createHook = , C extends Record> & C; - function useSelector

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; - function useSelector

(selector?: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean) { + function useSelector

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; + function useSelector

(selector?: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean) { return defaultHook(selector, compare); } diff --git a/packages/r-store/src/state/createState.ts b/packages/r-store/src/state/createState.ts index 49a41eb..67e4c9b 100644 --- a/packages/r-store/src/state/createState.ts +++ b/packages/r-store/src/state/createState.ts @@ -19,7 +19,7 @@ export type Setup = () => T; */ export type UseSelectorWithState = { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; /** * @deprecated * use `getReactiveState` / `getReadonlyState` instead @@ -29,22 +29,28 @@ export type UseSelectorWithState = { getLifeCycle: () => LifeCycle; getReactiveState: () => UnwrapNestedRefs; getReadonlyState: () => DeepReadonly>; + /** + * + * @param selector - a method to select the state, when the state change, the `cb` will be called + * @param cb - a callback function + * @returns a unsubscribe function + */ subscribe:

(selector: (state: DeepReadonly>) => P, cb?: () => void, shallow?: boolean) => () => void; useDeepSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useDeepStableSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useShallowSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useShallowStableSelector: { (): DeepReadonly> & C; -

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: P, next: P) => boolean): P; +

(selector: (state: DeepReadonly> & C) => P, compare?: (prev: Q, next: Q) => boolean): P; }; }; diff --git a/packages/r-store/src/store/createStore.ts b/packages/r-store/src/store/createStore.ts index be03900..f8be16b 100644 --- a/packages/r-store/src/store/createStore.ts +++ b/packages/r-store/src/store/createStore.ts @@ -11,7 +11,7 @@ export type { Creator } from "./_internal"; */ export type UseSelectorWithStore = { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; /** * @deprecated * use `getReactiveState` / `getReadonlyState` instead @@ -20,23 +20,29 @@ export type UseSelectorWithStore = { getLifeCycle: () => LifeCycle; getReactiveState: () => UnwrapNestedRefs; getReadonlyState: () => DeepReadonly>; + /** + * + * @param selector - a method to select the state, when the state change, the `cb` will be called + * @param cb - a callback function + * @returns a unsubscribe function + */ subscribe:

(selector: (state: DeepReadonly>) => P, cb?: () => void, shallow?: boolean) => () => void; useShallowSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useShallowStableSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useDeepSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; }; useDeepStableSelector: { (): DeepReadonly>; -

(selector: (state: DeepReadonly>) => P): P; - } +

(selector: (state: DeepReadonly>) => P, compare?: (prev: Q, next: Q) => boolean): P; + }; }; /**