mobx icon indicating copy to clipboard operation
mobx copied to clipboard

Use getTime() to check for Date equality

Open davetapley opened this issue 9 months ago • 4 comments

Intended outcome:

When updating observables from an API response which contains timestamps, if a timestamp in the response is unchanged since the last time I updated, I don't want autorun to trigger.

Actual outcome:

Because I cast the timestamps to a JS Date, autorun is triggered, even if the getTime() is the same.

How to reproduce the issue:

Open console on: https://codesandbox.io/s/autorun-new-date-2np493?file=/src/index.js

Observe:

  1. autorun doesn't log when clicking Finshed/Unfinished if already finished/unfinished
  2. 'Set now' buttons will always autorun because time is changing
  3. 'Set same' buttons differ in behavior: if 'Date' then will always autorun, if 'Time' then won't.

I'd argue that for 99.9999% of use cases you wouldn't want autorun to trigger, because the actual 'value' hasn't changed.

See also:

  • https://github.com/mobxjs/mobx-state-tree/issues/2080

davetapley avatar Sep 12 '23 20:09 davetapley