immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Programmatically identifying writable drafts vs plain objects?

matthewoates opened this issue Β· comments

commented

πŸ™‹β€β™‚ Question

How can you programmatically check if something is a writable draft as opposed to a plain object?

When I console.log my state provided by my redux slice's reducer argument I see something like:

      state: <ref *1> {
        type_: 0,
        scope_: {
          drafts_: [
            [Circular *1], [Object],
            [Array],       [Object],
            [Object],      [Object],
            [Object],      [Object],
            [Object],      [Array],
            [Object],      [Object],
            [Array]
          ],
          parent_: undefined,
          immer_: Immer {
// ... etc

but these properties (type_, scope_, ...) don't exist on the object that I logged. The constructor appears to be a plain Object, so I'm at a loss for runtime checking if an object is a writable draft.


Why am I even trying to do this? I'm using a helper function to lazily create a pseudo-random number generator using the writable draft state as a key (which also contains serialized data for the rng object) and I want to doubly make sure that I'm passing in the right data to this function.

commented

Oof. Lazy me. Works like a charm.