-
-
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
Fix #3728: observables initialization should not update state version #3732
Changes from 2 commits
4e18509
a0431e8
e9cc9c3
750eedb
20a7106
1323692
192e7c0
8a5595f
77e617c
6e81e91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"mobx": patch | ||
--- | ||
|
||
- fix: #3728: Observable initialization updates state version. | ||
- fix: Observable set initialization violates `enforceActions: "always"`. | ||
- fix: Changing keys of observable object does not respect `enforceActions`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,9 @@ export class Atom implements IAtom { | |
* Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate. | ||
*/ | ||
public reportChanged() { | ||
if (globalState.suppressReportChanged) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like a potentially dangerous check, that silently might break reactivity. E.g. do the following cases behave correctly? const x = observable({ a: 1 })
reaction(x => x.a, a => console.log("a", a))
extendObservable(x, { a: 2, b: 1 }) const x = observable({ a: 1 }, { proxy: true })
// react to new keys
reaction(x => Object.keys(x.a), a => console.log("a", a))
x.b = 1 I'm wondering if an alternative approach would be: only increase stateVersion if there are subscribers to the current atom? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point. Imo the play here is to make sure it's suppressed only on creation and not on mutation like in this case.
Purely from semantic perspective, two different states should aways have different versions, doesn't matter if someone was observing some part of the state or not. That's quite easy to reason about. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agreed, I think these principles weighs stronger than my concerns. Let's continue this direction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a fundamental problem with subclasses - What we would need is to treat all modifications of the same atom inside the same batch it was created as part of initialization. To do that we would need to introduce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, that sounds smart! It might serve in the future as well to better handle any weird edge cases around reaction creations in actions, recursive updates etc etc, although I believe we handle those all consistently now. |
||
return | ||
} | ||
startBatch() | ||
propagateChanged(this) | ||
// We could update state version only at the end of batch, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plan is to change these into
suppressReportChangedStart/End
and probably move it toinitObservableStart/End
(together withunstrackedStart/End
andallowStateChangesStart/End
as noted in comments).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, some dedicated utility sounds great here to avoid copy / paste mistakes.