+
Skip to content

Refactor: Adjust Coaction core for plain object state handling #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/core/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ export const create: Creator = <T extends CreateState>(
};
const getPureState: Store<T>['getPureState'] = () =>
internal.rootState as T;
const isSliceStore = typeof createState === 'object';
let isSliceStore = false;
if (
typeof createState === 'object' &&
createState !== null
) {
const values = Object.values(createState);
if (values.length > 0 && values.every((value) => typeof value === 'function')) {
isSliceStore = true;
}
// If values.length is 0 (empty object), or not all values are functions,
// and createState is an object, isSliceStore remains false.
// This means it's treated as a plain object state or an empty object.
}
// If createState is a function (not an object), isSliceStore also remains false.
Object.assign(store, {
name,
share: share ?? false,
Expand Down
43 changes: 22 additions & 21 deletions packages/core/src/getInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,37 @@ export const getInitialState = <T extends CreateState>(
internal: Internal<T>
) => {
const makeState = (
/**
* createState is a function to create the state object.
*/
createState: (
setState: (state: any) => void,
getState: () => any,
store: Store<T>
) => any,
/**
* the key of the slice state object.
*/
stateOrFn: ((setState: any, getState: any, store: Store<T>) => any) | object,
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a comment here to clearly explain that 'stateOrFn' can be either a slice creator function or a plain object state, reflecting its new dual role.

Copilot uses AI. Check for mistakes.

key?: string
) => {
// make sure createState is a function
if (
process.env.NODE_ENV !== 'production' &&
typeof createState !== 'function'
) {
throw new Error('createState should be a function');
let state: any; // Initialize state
if (typeof stateOrFn === 'function') {
// It's a slice creator function or a function returning state
state = stateOrFn(store.setState, store.getState, store);
} else if (typeof stateOrFn === 'object' && stateOrFn !== null) {
// It's a pre-existing object, potentially a third-party store instance or plain data
state = stateOrFn;
} else {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`Invalid state value encountered in makeState: ${key ? `for key ${key}, ` : ''}${typeof stateOrFn}`
);
}
return {}; // Return empty object or handle error appropriately
}
let state = createState(store.setState, store.getState, store);

// Preserve existing logic for unwrapping/handling state:
// support 3rd party library store like zustand, redux
if (state.getState) {
if (state && typeof state.getState === 'function') { // Add null/undefined check for state
state = state.getState();
// support 3rd party library store like pinia
} else if (typeof state === 'function') {
// This was for when a slice function returns another function (e.g. Pinia setup store).
// If stateOrFn was an object, and that object is itself a function, this would call it.
state = state();
}
// support bind store like mobx
if (state[bindSymbol]) {
if (state && state[bindSymbol]) { // Add null/undefined check for state
const rawState = state[bindSymbol].bind(state);
state[bindSymbol].handleStore(store, rawState, state, internal, key);
delete state[bindSymbol];
Expand All @@ -53,5 +54,5 @@ export const getInitialState = <T extends CreateState>(
}),
{} as ISlices<Slice<any>>
)
: makeState(createState as Slice<any>);
: makeState(createState as Slice<any> | object);
};
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载