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.

Passing equals option to computedFn does not change behaviour

See original GitHub issue

I’m trying to pass an options object to computedFn() with the equals property to set the comparer function to use.

filterForIndividual = computedFn(function(individualsId) {
    return this.filter((id, item) => ThObj.value(item, ThIdSessions.INDIVIDUALS_ID) === individualsId);
}, { equals: comparer.structural, });

The problem is that it always uses the comparer.default comparer. I have set a debug point in mobx.comparer.ts file and it never enters in the structuralComparer() method and always use defaultComparer().

I have tried to simplify the real code in a CodeSandbox and try to understand what is happening. The problem can be reproduced, no matter what the comparer value you set, the computedFn() always runs - so it is not caching - even if the returned data is the same (not same reference).

https://codesandbox.io/s/pedantic-yonath-owtvt?file=/src/App.js

Inspecting the source code seems that computedFn() can accept an options object and pass it throught to computed(), so there must be something I am missing .

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
DavidCasillasRiverocommented, Feb 11, 2021

Of course, you are right, I had this sentence from MobX and completely misunderstood it.

“Computed values can be used to derive information from other observables. They evaluate lazily, caching their output and only recomputing if one of the underlying observables has changed. If they are not observed by anything, they suspend entirely.”

I updated the codesandbox and the re-render only happens using comparer.identity as expected.

So the problem in my original code must be somewhere else, and the options passed to computedFn are working. Thanks for the super fast response.

1reaction
urugatorcommented, Feb 11, 2021

Try:

const t = new Test();

autorun(() => {
  console.log("autorun");  
  const map = t.filterStructural()  
})

t.increment();
t.increment();
t.increment();

There is just a single “autorun” in console as opposed to using t.filterIdentity (4).

Read more comments on GitHub >

github_iconTop Results From Across the Web

mobx-utils/computedFn.ts at master - GitHub
* and memoizes the output of the function based on the arguments passed in. *. * computedFn(fn) returns a function with the very...
Read more >
mobxjs/mobx - Gitter
I've looked at the source, and a transformation is just something that extends computable, so if I could pass the computed options to...
Read more >
Deriving information with computeds - MobX
computed(fn, options?) Computed values can be used to derive information from other observables. They evaluate lazily, caching their output and only recomputing ...
Read more >
Adding Computed Values to MobX Observable Classes
It won't be computed again if none of the Observable values that derive ... options that we can set to change the computed...
Read more >
mobxjs - Bountysource
I do my first little baby steps with mobx / MST and can't get ... I'm trying to pass an options object to...
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