question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unnecessary mutations

See original GitHub issue

I’m studying Overmind and everything seems so carefully planned that one thing surprised me: mutating the same part of the state with the same value does trigger a new mutation.

I’ll show it with an example:

// store.js
const overmind = new Overmind({
  onInitialize: ({ value }) => {
    value.addMutationListener(mutation => console.log(mutation));
  },
  state: {
    name: 'John'
  },
  actions: {
    changeName: ({ state }) => {
      state.name = 'Peter';
    }
  }
});

// Name.js
function Name() {
  const { state } = useOvermind();
  console.log('<Name /> rendered');
  return <h1>Hello {state.name}</h1>;
}

// App.js
function App() {
  const { actions } = useOvermind();
  return (
    <div className="App">
      <Name />
      <button onClick={actions.changeName}>Change name to Peter</button>
    </div>
  );
}

Clicking on the <button> triggers a new mutation and a new (unnecessary) re-render of the <Name /> component.

Logs are:

Object {method: "set", path: "name", args: ["Peter"]}
<Name /> rendered
Object {method: "set", path: "name", args: ["Peter"]}
<Name /> rendered
Object {method: "set", path: "name", args: ["Peter"]}
<Name /> rendered
...

codesandbox: https://codesandbox.io/s/7wjj165ynq

I saw the same behaviour in ProxyStateTree: https://codesandbox.io/s/3846ww49qq (I modified the toggleItemCompleted action)

I’m used to Mobx, which doesn’t trigger mutations if the value doesn’t change, not matter what you do.

How’s this going to work in Overmind/ProxyStateTree? Have you already thought about the overhead of comparing those scalar values vs the performance gains derived from avoiding those unnecessary re-renders?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

9reactions
christianalfonicommented, Feb 22, 2019

Cool, will look into this for next release 😃

3reactions
luisherranzcommented, Feb 13, 2019

Does that make sense?

Yes, it makes perfect sense 😃

This information can also be used to tell the devtools that the old and new value is the same

I’d even show previous and current value for each mutation. I remember that in Redux Devtools and it was very useful for debugging:

screen shot 2019-02-11 at 12 18 16

When proxy-state-tree “flushes” out changes it will ignore mutations where the old and new value is the same

The last doubt I have is, can proxy-state-tree traverse the state object to check for changes in scalar values or would it work like an immutable store, where the whole object is thrown away when the reference changes?

This is what I mean:

const state = { user: { firstName: 'Jon', lastName: 'Snow' } }

const changeUser = ({ state }, user) => {
  state.user = user
}

// trigger mutation:
changeUser({ firstName: 'Jon', lastName: 'Targaryen' })

// current mutation:
{ method: "set", path: "user", args: [{ firstName: 'Jon', lastName: 'Targaryen' } }]

// ideal (minimal) mutation:
{ method: "set", path: "user.lastName", args: ['Targaryen'] }

I’m not sure yet how the internals of proxy-state-tree work and how difficult would be to do this type of minimal mutation but, what are your thoughts about this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Genetic Mutations: Overview & Types - Cleveland Clinic
Genetic mutations are changes to your DNA sequence that happen during cell division when your cells make copies of themselves.
Read more >
A meta-analysis of nonsense mutations causing human ...
Nonsense mutations account for approximately 11% of all described gene lesions causing human inherited disease and approximately 20% of disease-associated ...
Read more >
10 Most Useless Human Mutations - YouTube
Sometimes people are born with genetic mutations that may hinder or even benefit their lives. Today however we're looking at the most ...
Read more >
CRISPR causes many unwanted mutations, small study ...
Does the CRISPR gene-editing method cause hundreds of extra, unwanted mutations? That's the question raised by a small study in mice.
Read more >
Stem Cell Lines Riddled With Undetected Mutations
A cell line can harbor thousands of mutations and still be usable for research as long as those mutations are concentrated in irrelevant ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found