8000 Refactor: Adjust Coaction core for plain object state handling by unadlib · Pull Request #2 · unadlib/coaction · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

unadlib
Copy link
Owner
@unadlib unadlib commented May 20, 2025

This commit refines how Coaction's core create 8000 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.

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.
Copy link

Coverage after merging fix/coaction-plain-object-slices into main will be

84.62%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/coaction-mobx/src
   index.ts94.12%80%100%100%13, 22, 27
packages/coaction-pinia/src
   index.ts80.72%73.68%71.43%86%101–102, 114, 56, 68–72, 76, 78, 82
packages/coaction-zustand/src
   index.ts43.02%15%71.43%46.15%19–20, 20, 20–26, 26, 26–27, 29, 29, 29–31, 31, 31, 33, 38, 40, 40, 40–46, 49, 52, 54, 59–61, 61, 61–63, 63, 63, 65, 90
packages/core/dist
   coaction.cjs.js80%50%100%100%11
packages/core/src
   applyMiddlewares.ts58.33%25%100%66.67%10, 8–9, 9, 9
   asyncClientStore.ts83.33%70%85.71%90.91%25, 27, 27, 45–46, 52, 59, 65, 98
   binder.ts100%100%100%100%
   computed.ts100%100%100%100%
   constant.ts71.43%50%100%100%3, 5
   create.ts85.98%81.25%90%89.80%131–132, 34–35, 35, 39, 59, 59–60, 60, 60–61, 63, 95
   getInitialState.ts69.57%59.09%100%76.19%15, 18, 18, 18, 18, 18, 20, 22, 22, 22–24, 24, 27
   getRawState.ts86.10%85.45%87.50%86.11%109–112, 117, 117, 117–118, 124–126, 126, 126–129, 44–45, 85–87, 93–94
   global.ts41.18%16.67%100%50%10, 3, 5, 5, 5–7, 7, 7–8
   handleMainTransport.ts82.22%77.78%100%83.33%30, 36–37, 52, 52–53, 58–59
   handleState.ts95.62%91.84%100%97.40%36–37, 77, 80, 93–94
   index.ts100%100%100%100%
   utils.ts75.86%61.54%100%85.19%25–26, 26, 26, 26, 26–28, 28, 28, 28, 28–29, 5
   wrapStore.ts100%100%100%100%
packages/logger/src
   index.ts100%100%100%100%
   logger.ts67.80%53.73%87.50%86.05%104–106, 106–107, 12, 123, 125–127, 133–134, 137–138, 143–144, 148–150, 59–60, 65, 65, 65–67, 67, 72, 72, 75, 83–85, 85–86, 96–97

@unadlib unadlib requested a review from Copilot May 20, 2025 17:17
Copy link
@Copilot Copilot AI left a 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,
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0