Skip to content

Commit 4118443

Browse files
improve check
1 parent ce11a15 commit 4118443

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

packages/r-store/src/__test__/type.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,22 @@ useCountG.getActions();
153153
useCountG.getReactiveState().count;
154154

155155
const useHH1 = createState(
156-
withActions(
157-
withNamespace(
158-
withNamespace(() => ({ data: [] as number[] }), { namespace: "122" }),
159-
{ namespace: "111" }
156+
withActions(withNamespace(
157+
withActions(
158+
withActions(
159+
// withNamespace(
160+
() => ({ data: [] as number[] }),
161+
// { namespace: "111" }
162+
// ),
163+
{ generateActions: (s) => ({ del: () => s.data.pop() }) }
164+
),
165+
{ generateActions: (state) => ({ ff: () => state.data.push(9999) }) }
160166
),
161-
{ generateActions: (s) => ({ del: () => s.data.pop() }) }
162-
),
167+
{ namespace: "hhh" }
168+
), {generateActions: (s) => ({lll: () => s.data.sort()}), automaticBatchAction: true}),
163169
{
164-
// withActions: (state) => ({ add: () => state.data.push(100) }),
165-
withNamespace: "1111",
170+
withActions: (state) => ({ add: () => state.data.push(100), kkkk: () => state.data.sort() }),
171+
// withNamespace: "1111",
166172
}
167173
// {
168174
// withActions: (s) => ({add: () => s.data.push(199)}),
@@ -190,7 +196,7 @@ useHH2.getReactiveState();
190196
const usePosition = () => {
191197
const [state, setState] = useReactiveState({ x: 0, y: 0 });
192198

193-
const [xPosition, setXPosition] = useReactiveState({ x: 0 });
199+
const [xPosition, setXPosition] = useReactiveState(() => ({ x: 0 }));
194200

195201
useReactiveEffect(() => {
196202
const listener = (e: MouseEvent) => {

packages/r-store/src/state/_internal.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { reactive, toRaw } from "@vue/reactivity";
2+
import { isPromise } from "@vue/shared";
23

34
import { connectDevTool } from "../shared/dev";
45
import { isServer } from "../shared/env";
@@ -47,6 +48,13 @@ export function internalCreateState<T extends Record<string, unknown>, P extends
4748
// handle withActions middleware;
4849
const initialState = getFinalState(state) as T;
4950

51+
if (__DEV__ && isPromise(initialState)) {
52+
console.error(
53+
`[reactivity-store] '${name}' expect receive a plain object but got a promise %o, this is a unexpected usage. should not return a promise in this 'setup' function`,
54+
initialState
55+
);
56+
}
57+
5058
let actions = getFinalActions(state);
5159

5260
const namespace = getFinalNamespace(state);

packages/r-store/src/store/_internal.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { reactive, toRaw } from "@vue/reactivity";
2+
import { isPromise } from "@vue/shared";
23

34
import { createHook } from "../shared/hook";
45
import { createLifeCycle } from "../shared/lifeCycle";
@@ -17,12 +18,19 @@ export type Creator<T extends Record<string, unknown>> = () => T;
1718
export const internalCreateStore = <T extends Record<string, unknown>>(creator: Creator<T>, name = "createStore", lifeCycle?: LifeCycle) => {
1819
const state = creator();
1920

21+
if (__DEV__ && isPromise(state)) {
22+
console.error(
23+
`[reactivity-store] '${name}' expect receive a reactive object but got a promise %o, this is a unexpected usage. should not return a promise in this 'creator' function`,
24+
state
25+
);
26+
}
27+
2028
if (__DEV__ && checkHasMiddleware(state)) {
2129
console.error(`[reactivity-store] '${name}' not support middleware usage, please change to use 'createState'`);
2230
}
2331

2432
if (__DEV__ && !checkHasReactive(state)) {
25-
console.error(`[reactivity-store] '${name}' expect receive a reactive object but got a plain object %o, this is a unexpected usage`, state);
33+
console.error(`[reactivity-store] '${name}' expect receive a reactive object but got a plain object %o, this is a unexpected usage. should return a reactive object in this 'creator' function`, state);
2634
}
2735

2836
const rawState = toRaw(state);

0 commit comments

Comments
 (0)