-
Notifications
You must be signed in to change notification settings - Fork 1
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
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
base: main
Are you sure you want to change the base?
Conversation
This commit refines how Coaction's core `create` function handles plain JavaScript objects provided as `createState`. Previously, if `createState` was any object, Coaction would set `isSliceStore = true`, incorrectly attempting to apply its native "slices pattern" (where each object property is a slice-defining function). This led to errors when you provided simple state objects (e.g., from third-party libraries or direct state) as it would try to execute non-function properties. The following changes have been made: 1. **Refined `isSliceStore` Logic (`packages/core/src/create.ts`):** `isSliceStore` is now only `true` if `createState` is an object AND all its enumerable property values are functions. For plain objects (including empty ones) or objects with non-function properties, `isSliceStore` is `false`. 2. **Modified `makeState` Function (`packages/core/src/getInitialState.ts`):** The `makeState` function now accepts either a slice-creator function or a direct object as its input. This allows it to correctly process plain objects passed as state, while still handling its original role of executing slice functions and unwrapping third-party store instances (e.g., those with `.getState()` or function stores like Pinia). 3. **Adjusted Main Logic in `getInitialState.ts`:** The main part of `getInitialState` now correctly passes plain objects to `makeState` when `isSliceStore` is `false`. These changes ensure that Coaction no longer misinterprets plain state objects as its native functional slice maps, allowing for more robust and intuitive state initialization, especially when working with state from third-party libraries or defining simple, direct state. **Testing Caveat:** Unit tests for these changes were added to `packages/core/test/create.test.ts`. However, due to persistent Jest/Babel configuration issues within the `packages/core` test environment, these new tests, along with existing ones in that package, could not be executed to confirm pass status at the time of this commit. The logical changes have been reviewed and are submitted with this understanding.
Coverage after merging fix/coaction-plain-object-slices into main will be
Coverage Report
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors how Coaction's core handles state initialization by clearly distinguishing between plain object states and slice creator functions.
- Refines the logic in getInitialState to support both function-based and plain object states.
- Adjusts create.ts to correctly set isSliceStore only when all enumerable properties are functions.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
packages/core/src/getInitialState.ts | Adapts makeState to accept either a function or an object; adds null checks and updated error handling for invalid state types. |
packages/core/src/create.ts | Revises isSliceStore logic to inspect object properties before determination. |
Comments suppressed due to low confidence (1)
packages/core/src/getInitialState.ts:22
- [nitpick] In production the logic returns an empty object for an invalid state type, which may silently mask issues. Consider documenting the rationale for this design decision or adding a warning log.
if (process.env.NODE_ENV !== 'production') { throw new Error(...
/** | ||
* the key of the slice state object. | ||
*/ | ||
stateOrFn: ((setState: any, getState: any, store: Store<T>) => any) | object, |
There was a problem hiding this comment.
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.
This commit refines how Coaction's core
create
8000 function handles plain JavaScript objects provided ascreateState
.Previously, if
createState
was any object, Coaction would setisSliceStore = true
, incorrectly attempting to apply its native "slices pattern" (where each object property is a slice-defining function). This led to errors when you provided simple state objects (e.g., from third-party libraries or direct state) as it would try to execute non-function properties.The following changes have been made:
Refined
isSliceStore
Logic (packages/core/src/create.ts
):isSliceStore
is now onlytrue
ifcreateState
is an object AND all its enumerable property values are functions. For plain objects (including empty ones) or objects with non-function properties,isSliceStore
isfalse
.Modified
makeState
Function (packages/core/src/getInitialState.ts
): ThemakeState
function now accepts either a slice-creator function or a direct object as its input. This allows it to correctly process plain objects passed as state, while still handling its original role of executing slice functions and unwrapping third-party store instances (e.g., those with.getState()
or function stores like Pinia).Adjusted Main Logic in
getInitialState.ts
: The main part ofgetInitialState
now correctly passes plain objects tomakeState
whenisSliceStore
isfalse
.These changes ensure that Coaction no longer misinterprets plain state objects as its native functional slice maps, allowing for more robust and intuitive state initialization, especially when working with state from third-party libraries or defining simple, direct state.
Testing Caveat:
Unit tests for these changes were added to
packages/core/test/create.test.ts
. However, due to persistent Jest/Babel configuration issues within thepackages/core
test environment, these new tests, along with existing ones in that package, could not be executed to confirm pass status at the time of this commit. The logical changes have been reviewed and are submitted with this understanding.