-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use getTime() to check for Date equality #3757
Comments
Do the check before mutating the observable. const date = new Date(whatever)
o.date = date;
assert(date === o.date); // always true EDIT: Correction, default comparer seems to be mobx/packages/mobx/src/utils/comparer.ts Lines 19 to 25 in 91adf79
but the point is the same |
Well, sure that would work, but I think MobX should do it internally because: a. For 99.9999% of use cases you wouldn't want I'm not sure how your snippet ⬇️ fits in? const date = new Date(whatever)
o.date = date;
assert(date === o.date); // always true Yes, that is true, but what's relevant is: const date = new Date(whatever)
o.date = date; // triggers autorun ✅
o.date = date; // does not trigger autorun ✅
o.date = new Date(whatever) // triggers autorun ⛔️ A patten which is quite likely if |
Autorun doesn't do any value diffing. The comparison is done when setting a value and if it's equal it's completely ignored - it's not set and autorun isn't notified. Autorun is guaranteed to run if state changed, so we can't mutate state without notifying autorun, we must do both or neither.
It's demonstrating an expectation, which would be broken if we would use a different comparison for date objects. The assigment operation would have a surprising behavior.
Can be said about any value like object, both builtin or proprietary. This also includes collections of value objects (array of dates etc). Btw we expose mobx/packages/mobx/src/utils/eq.ts Line 47 in 91adf79
Date isn't primitive, similary to |
Thanks for reply / clarifications @urugator. I put most of my thoughts here: mobxjs/mobx-state-tree#2080 (comment) Thanks for mentioning |
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:
autorun
doesn't log when clicking Finshed/Unfinished if already finished/unfinishedautorun
because time is changingautorun
, 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:
The text was updated successfully, but these errors were encountered: