You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR fixes the handling of function-valued options in store.set() to correctly distinguish between:
Updater functions: Functions passed to update primitive/object values (should be invoked with current value)
Function values: Functions stored as actual state values (should NOT be invoked)
Problem
Previously, store.set() would invoke ANY function passed as a value, making it impossible to store function references as state values. This caused issues when trying to store callbacks, handlers, or other function-valued options.
Solution
Added logic to detect when a value should be treated as an updater function vs. a function value by checking:
If the passed value is a function
If the previous value exists and is not null
If the previous value is NOT already a function
This ensures:
Updater callbacks work correctly: store.set('count', (prev) => prev + 1)
Function values are stored directly: store.set('handler', () => 'result')
Changes
Modified createBaseApi.ts:44-52 to add conditional logic for updater invocation
Added test coverage for both callback updates and function value storage
Added changeset documenting the fix
Test Coverage
✅ Callback updates for primitive values still work
✅ Function values can be stored without invocation
✅ Null/undefined function values handled correctly
✅ Documentation: Changeset properly describes the fix
✅ Edge Cases: Handles undefined, null, and function-valued prevValue
✅ Code Style: Follows project conventions
✅ No Breaking Changes: Pure bug fix, maintains API contract
Potential Considerations
💡 Suggestion - Documentation Enhancement (Optional):
Consider adding JSDoc comments to the shouldInvokeUpdater logic explaining the behavior:
// Only invoke as updater when:// 1. Value is a function// 2. Previous value exists (not undefined/null) // 3. Previous value is NOT a function (to allow storing function values)constshouldInvokeUpdater=/* ... */
💡 Edge Case Question:
What happens when trying to use an updater function on an initial undefined value?
Example: store.set('newKey', (prev) => prev + 1) where newKey doesn't exist yet?
Current logic: Won't invoke (prevValue is undefined) ✅ Correct behavior
Final Assessment
Status: ✅ Approved - Excellent Implementation
This is a well-thought-out fix that:
Solves the core problem elegantly
Maintains full backward compatibility
Includes comprehensive test coverage
Handles all edge cases correctly
Follows clean code principles
No blocking issues identified. The implementation is production-ready.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the handling of function-valued options in
store.set()to correctly distinguish between:Problem
Previously,
store.set()would invoke ANY function passed as a value, making it impossible to store function references as state values. This caused issues when trying to store callbacks, handlers, or other function-valued options.Solution
Added logic to detect when a value should be treated as an updater function vs. a function value by checking:
This ensures:
store.set('count', (prev) => prev + 1)store.set('handler', () => 'result')Changes
createBaseApi.ts:44-52to add conditional logic for updater invocationTest Coverage
🤖 Generated with Claude Code