-
Notifications
You must be signed in to change notification settings - Fork 19
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 onChange callbacks #72
Comments
Just migrated to mutative days ago, and this feature is EXACTLY what i need. When editing data in my UI I currently have an internal custom "proxyObserver" wrapping a clone of my data and giving me onChange callbacks (for showing if data is changed). But with this I can replace my proxyObserver and also generate patches and minimize the mutations in the changed data. |
hi @atsjo, I may start implementing this API soon. |
Made it work with the current version by wrapping the draft in my custom proxyObserver, so using double proxy for the state... |
These drafts are not finalized, which means they might require shallow copying under the immutable mechanism, and the draft instances will not be garbage collected. I don’t really recommend doing this. It seems like the |
ok, thanks for the info! I normally use it like a normal producer, but also have a forms based ui bound to a json structure, where this is used to generate the diff from last saved state. When saving I generate the diff via finalize, but I didn't bother finalizing the draft if the user cancels the edit session... My proxyObserver is just for getting callbacks when users change anything via the ui, so I can show if there are modifications and show the save button... If I could get a callback on every change from mutative I wouldn't need it... I'll just ensure finalize is called also when users cancels the changes.... |
hi @atsjo , I suggest using import { create, original, rawReturn } from 'mutative';
const nextState = create(initState, (draft) => {
// some logic about `shouldBeChanged`
if (!shouldBeChanged) {
return rawReturn(original(draft));
}
}); |
Proposal
I propose adding support for a mutation callback feature to trigger state change events. This would allow developers to provide a callback function to track mutations (e.g., state changes, patches, and inverse patches) during a state update.
Use Case
This feature would be especially helpful for:
Suggested API
Here’s an example of how this could work:
Callback Function Signature:
Parameters:
Implementation Notes
This API would be backward-compatible and opt-in, ensuring it does not affect existing users who do not need the onChange functionality.
Benefits
The text was updated successfully, but these errors were encountered: