Skip to content

Handling of shared objects (>1 object reference) #161

@joshkel

Description

@joshkel

According to #159:

Having more than one path from the root to the same node without introducing a cycle (i.e. a DAG with shared nodes) is fine and is something Mutative supports.

Internally, each underlying source object is drafted at most once, and all references to that object share the same draft. Mutating it through one path is reflected through any other path that points to the same object.

However, this doesn't seem to work in my testing:

  const obj = {};
  obj.color1 = obj.color2 = { name: 'Red' };
  console.log('Same object:', obj.color1 === obj.color2);
  const result = create(obj, (draft) => {
    draft.color1.name = 'Blue';
    console.log('Same object within mutate:', draft.color1 === draft.color2);
  });
  console.log('Same object after:', result.color1 === result.color2);
  console.log(result);

Output:

Same object: true
Same object within mutate: false
Same object after: false
{ color2: { name: 'Red' }, color1: { name: 'Blue' } }

Based on the discussion in #159, I expected it to be the same object each time, and I expected the result to be { color1: { name: 'Blue' }, color2: { name: 'Blue' } }.

It's also possible that I've misunderstood or am making some other mistake.

This issue isn't immediately affecting me, but it seems inconsistent with previous discussions, and I'd like to at least understand what's going on.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions