mutative
mutative copied to clipboard
Proposal: Add support for mutable data in apply() function
Overview
Currently, the apply() function in Mutative is primarily designed to work with immutable data structures. While there are workarounds to handle mutable data indirectly, direct support for mutable data structures could enhance the flexibility and usefulness of the apply() function.
Proposal
We propose extending the apply() function to directly support mutable data structures, allowing users to apply patches to both mutable and immutable data without requiring additional wrapper functions or conversions.
Motivation
- Improve flexibility: Allow users to work seamlessly with both mutable and immutable data structures.
- Reduce complexity: Eliminate the need for workarounds when dealing with mutable data.
- Enhance performance: Direct support for mutable data(e.g. observable state) could potentially offer performance benefits in certain scenarios.
This allows observable states to support patches.
Possible Implementation
- Add a new option to the
apply()function, e.g.,mutable: boolean. - When
mutableis true, modify the internal logic ofapply()to handle mutable data structures differently:- Skip creating new drafts when applying patches.
- Directly modify the input data structure.
Considerations
- Implement safeguards: Prevent unintended mutations of immutable parts of the structure.
- Backward compatibility: Ensure that the default behavior remains unchanged for users who rely on the current immutable-only functionality.
- Performance impact: Evaluate any potential performance implications of adding this feature.
- Type safety: Ensure that TypeScript types are updated to reflect the new functionality.
Questions for Discussion
- Should this be implemented as a separate function (e.g.,
applyMutable()) instead of an option in the existingapply()function? - Are there any security concerns with allowing direct mutation of data structures if immutable data is mixed?
- How should this feature interact with other Mutative features like auto-freezing and strict mode if immutable data is mixed?
Next Steps
If this proposal is accepted:
- Create a detailed technical specification.
- Implement a prototype and benchmark its performance.
- Update documentation to reflect the new functionality.
- Add comprehensive test cases to cover mutable data scenarios.
We welcome feedback and discussion on this proposal from the Mutative community.