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

How to log modified draft state? #591

Closed
dimaqq opened this issue Apr 30, 2020 · 14 comments
Closed

How to log modified draft state? #591

dimaqq opened this issue Apr 30, 2020 · 14 comments
Labels

Comments

@dimaqq
Copy link

dimaqq commented Apr 30, 2020

🙋‍♂ Question

I want to log a complex draft modification (for debug, in react-app-rewired script).

Given e.g.:

module.exports = (config, env) => immer.produce(config, draft => {
  draft.foo.bar = baz;
  call.some.mutating.function(draft);

  console.log(immer.original(draft));  // this I can do
  console.log(immer.resulting(draft));  // this I cannot do
});

I know I can log the `draft` itself, but it's pretty messy and seemingly unusable for deeply nested data.
@mweststrate
Copy link
Collaborator

mweststrate commented Apr 30, 2020 via email

@dimaqq
Copy link
Author

dimaqq commented Apr 30, 2020

Yes that works, thank you!
And thus, I guess I can always do JSON.parse(JSON.stringify(draft)) to get a PDO :)

@mweststrate
Copy link
Collaborator

mweststrate commented Apr 30, 2020 via email

@dimaqq
Copy link
Author

dimaqq commented May 1, 2020

Weirdly, the JSON.stringify hack (over webpack config) doesn't work with CRA yarn start...
My setup is CRA + react-app-rewired + my custom config-overrides.js.
Specifically, just adding void(JSON.stringify(draft)); where draft is webpack config, like {mode: "development", plugins: ...} makes yarn start fail with:

Failed to compile.

Cannot add property hidePathInfo, object is not extensible

error Command failed with exit code 1.

I wonder if applying JSON to the draft somehow changes the draft internally. 🤔

@mweststrate
Copy link
Collaborator

mweststrate commented May 1, 2020 via email

@dimaqq
Copy link
Author

dimaqq commented May 1, 2020

Yes, immer.setAutoFreeze(false) at module top level, before the invocation of immer.produce solve this!

@dimaqq
Copy link
Author

dimaqq commented May 18, 2020

On a related note, I think I've found a case where it would be nice, though not strictly required to have live access to modified state.

I'd like to be able to write something like this:

case "SET_SEARCH":
  draft.search_terms = action.search;
  draft.search_hits = search(draft.data, draft.search_index, draft.search_terms);
case "SET_DATA":
  draft.data = action.data;
  draft.search_hits = search(draft.data, draft.search_index, draft.search_terms);
case "SET_INDEX":
  draft.search_index = action.index;
  draft.search_hits = search(draft.data, draft.search_index, draft.search_terms);

However, that fails because search is dumb and cannot handle Proxy objects.

So, for now I have to write

case "SET_SEARCH":
  draft.search_terms = action.search;
  draft.search_hits = search(original(draft.data), original(draft.search_index), action.search);
case "SET_DATA":
  draft.data = action.data;
  draft.search_hits = search(action.data, original(draft.search_index), original(draft.search_terms));
case "SET_INDEX":
  draft.search_index = action.index;
  draft.search_hits = search(original(draft.data), action.index, original(draft.search_terms));

Which is explicit 👍but wonky and search calls don't look the same 👎

P.S. I don't want to use JSON.parse(JSON.stringify(draft.data)) because data is quite large.

@mweststrate
Copy link
Collaborator

mweststrate commented May 18, 2020 via email

@dimaqq
Copy link
Author

dimaqq commented May 19, 2020

I can't reproduce the proxy making a difference any more 🤔
Perhaps I had some other bug at the time...

When I'm comparing calling my search with and without original(...), all I can see is:

  • passing draft is at least 2x slower than original(draft) over any data I try
  • sometimes, passing draft is 16x slower than original(draft)

I'm testing this with a data set of 4K objects, each having ~4 searchable fields, and in practice the performance is pretty good with or without original. I ought to support 100K objects though, so I'm trying to be proactive.

@mweststrate
Copy link
Collaborator

mweststrate commented May 19, 2020 via email

@mweststrate
Copy link
Collaborator

added some more explicit performance tips here: 4af88f1

@mweststrate
Copy link
Collaborator

With Immer 7, it will be possible to call console.log(current(draft)) which will be a lot more efficient than stringifying.

mweststrate added a commit that referenced this issue Jun 10, 2020
* Introduced `current`, which takes a snapshot of the current state of a draft and finalizes it (but without freezing). Current is a great utility to print the current state during debugging (no Proxies in the way), and the output of current can also be safely leaked outside the producer. Implements #441, #591
* [BREAKING CHANGE] getters and setters are now handled consistently: own getters and setters will always by copied into fields (like Object.assign does), inherited getters and setters will be left as-is. This should allow using Immer directly on objects that trap their fields, like down in Vue or MobX. Fixes #584, #439, #593, #558
* [BREAKING CHANGE] produce no longer accepts  non-draftable objects as first argument
* [BREAKING CHANGE] original can only be called on drafts and will throw otherwise (fixes #605)
* [BREAKING CHANGE] non-enumerable and symbolic fields will never be frozen
* [BREAKING CHANGE] the patches for arrays are now computed differently to fix some scenarios in which they were incorrect. In some cases they will be more optimal now, in other cases less. Especially splicing / unshifting items into an existing array might result in a lot of patches. Fixes #468
* Improved documentation in several areas, there is now a page for typical update patterns and a separate page on how to work with classes. And additional performance tips have been included. Fixes #457, #115, #462
* Fixed #462: All branches of the produced state should be frozen
* Fixed #588: Inconsistent behavior with nested produce
* Fixed #577: Immer might not work with polyfilled symbols
* Fixed #514, #609: Explicitly calling `useProxies(false)` shouldn’t check for the presence of Proxy.
@eric-burel
Copy link

Hi, is there any hope to have a better display in Vs Code debugger? A comment in this Stack Overflow post describes how to use a Watcher to do this, but that's something you have to set manually: immer__WEBPACK_IMPORTED_MODULE_1__.current(draft).

What would be needed to get a better display in this situation?

@mweststrate
Copy link
Collaborator

mweststrate commented Jan 19, 2022 via email

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

No branches or pull requests

3 participants