Skip to content
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

Proposal: Add support for mutable data in apply() function #54

Open
unadlib opened this issue Sep 4, 2024 · 0 comments
Open

Proposal: Add support for mutable data in apply() function #54

unadlib opened this issue Sep 4, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@unadlib
Copy link
Owner

unadlib commented Sep 4, 2024

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

  1. Add a new option to the apply() function, e.g., mutable: boolean.
  2. When mutable is true, modify the internal logic of apply() 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

  1. Should this be implemented as a separate function (e.g., applyMutable()) instead of an option in the existing apply() function?
  2. Are there any security concerns with allowing direct mutation of data structures if immutable data is mixed?
  3. 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:

  1. Create a detailed technical specification.
  2. Implement a prototype and benchmark its performance.
  3. Update documentation to reflect the new functionality.
  4. Add comprehensive test cases to cover mutable data scenarios.

We welcome feedback and discussion on this proposal from the Mutative community.

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

No branches or pull requests

1 participant