-
-
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
feat: derivationRequiresObservable & observablesRequiresReaction #2079
Conversation
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.
Great stuff indeed. Got myself trapped by both of these caveats couple of times too.
8a253eb
to
bc95364
Compare
I'm not sure how to update the changelog, do we have any script that do that, or manually? |
Still manually. And tick other checkboxes as well when completed :) |
I see that |
I've pushed the changelog update |
Change observablesRequiresReaction to observableRequiresReaction Change derivationRequiresObservable to reactionRequiresObservable
Note on defaults:
Could you add some test cases where observables are accesses indirectly (through computed) or inside action (eg inside autorun)? |
Can you please elaborate?
I personally think it's not a good practice, and the developer should be aware to what gonna render and why.
I will |
Ability to configure this per OT:
Why not to use explicit subscription then? |
I also don't agree, why would someone want to deoptimize on purpose? I think it comes more from the current lack of validation fixed by this PR that kinda forces you to just "slap observer everywhere". I don't see an actual reason for doing it otherwise. That said, with |
The lib is all about automatic subscription management. The idea is that you don't have to worry about what depends on what and you shouldn't experience staleness once you fulfill the contract -
Agree, but as mentioned the fix is not generally applicable, eg it makes sense only with What's the expected behavior of: mobx.configure({ observablesRequiresReaction: true })
autorun(() => {
untracked(() => x.y);
}) Can you add it to tests as well please? How do you feel about labelling this "experimental" and waiting for some feedback? |
I prefer to keep it optional and it's totally can be marked experimental so we could tweak the behaviour later without breaking changes.
We will need to also change mobx-react api to expose also that |
Ignore on production for performance sake I suppose... |
Another thing that came up in my mind: |
The change can be conditional or deferred: @action doSomething(a) {
if (this.cond) {
this.x = a;
}
}
@action doSomething() {
// action is untracked to avoid accidental subscription (so it's not entirely useless even if you don't change anything):
const param = this.y;
xhr.post(param).then(() => this.x = "a");
} |
changes pushed |
aba2323
to
aa58748
Compare
@urugator friendly ping :) |
Just to clarify... I didn't mind that it didn't warn inside |
@mweststrate you may want to take a look at this. To summarize:
|
Thanks for summarising! Appreciate it :) Both features sound pretty cool
for debugging purposes. Didn't review the code, but I think these are good
features to have. Naming is hard indeed, but at least there is a cool
symmetry :-P.
…On Tue, Sep 24, 2019 at 5:10 PM urugator ***@***.***> wrote:
@mweststrate <https://github.com/mweststrate> you may want to take a look
at this.
To summarize:
2 new global configuration options:
reactionRequiresObservable: boolean = false
- warns when derivation.observing.length === 0
- also configurable per individual reaction/autorun/when (via
requiresObservable option)
- motivation: help the developer to be aware of the reactive parts of
the code, help to catch cases when you touch the observable only after if
that wasn't true on the first run.
- implemented as simple check inside track function
observableRequiresReaction: boolean = false
- warns when any observable is acccessed outside of derivation or
action
- motivation: reveal dereferencing outside of observer (eg passing to
3rd party lib, forgetting observer, dereferencing to soon, etc)
- implemented similary as enforceAction - global flag allowStateReads,
switched in start(end)Action/trackDerivedFunction, checked in
reportObserved
- unsure about the option name, the most descriptive name would be
readingObservableRequiresReactionOrAction, input welcome (
enforceReaction, ...??)
Both currently labelled as experimental.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2079?email_source=notifications&email_token=AAN4NBEJORWESCKTRRTUN53QLI3XPA5CNFSM4IMTODJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7O5B5Q#issuecomment-534630646>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAN4NBBLD6ULWVLLOX3HTBLQLI3XPANCNFSM4IMTODJA>
.
|
7ed2f27
to
371a733
Compare
371a733
to
abf4f15
Compare
I've updated the mobx 4 backport PR |
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.
Looking great so far! Left some comments, but nothing big :)
Sure, feel free to merge! I'll add a note to self to tinker another time
about messaging
…On Fri, Sep 27, 2019 at 9:58 AM Bnaya Peretz ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/core/derivation.ts
<#2079 (comment)>:
> @@ -155,12 +160,23 @@ export function checkIfStateModificationsAreAllowed(atom: IAtom) {
)
}
+export function checkIfStateReadsAreAllowed(observable: IObservable) {
+ if (
+ process.env.NODE_ENV !== "production" &&
+ !globalState.allowStateReads &&
+ globalState.observableRequiresReaction
+ ) {
+ console.warn(`[mobx] Observable ${observable.name} being read outside a reactive context`)
As these feature marked as experimental, i would prefer to push it forward
and get feedback, and then improve developer experience
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2079?email_source=notifications&email_token=AAN4NBG3SBGO3WI6NWR6KV3QLXDJTA5CNFSM4IMTODJKYY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCGERAAQ#discussion_r328975946>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAN4NBCLKG77JXG5PTCHMWLQLXDJTANCNFSM4IMTODJA>
.
|
c91bbbd
to
8c070a9
Compare
8c070a9
to
b1a30f5
Compare
I've merged the PRS! What's the policy regarding publishing? |
There is currently no policy :'). Just some ideas to incorporate `np` like
done in immer. But I'll try to cut a release somewhere today. TY!
…On Mon, Sep 30, 2019 at 2:20 PM Bnaya Peretz ***@***.***> wrote:
I've merged the PRS!
What's the policy regarding publishing?
also, i wish we had something that publishes canary versions for each PR
or so
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2079?email_source=notifications&email_token=AAN4NBC47N4MHAZBL27TXW3QMH4I7A5CNFSM4IMTODJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD75TMVA#issuecomment-536557140>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAN4NBCPFJVTDSILVFLTN2LQMH4I7ANCNFSM4IMTODJA>
.
|
Released! |
I've created a blog post regarding these features: |
Looks awesome! I think it is pretty clear. Make sure to link it in the
docs! Maybe a link on top of https://mobx.js.org/best/trace.html would be
neat as well.
…On Wed, Oct 2, 2019 at 11:52 PM Bnaya Peretz ***@***.***> wrote:
I've created a blog post regarding these features:
***@***.***/mobx-5-14-0-will-help-you-stay-reactive-and-only-when-you-need-to-2e897d3bfabe
I would love to have some feedback before publishing
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2079?email_source=notifications&email_token=AAN4NBFWFUO6VS5SBTRBAWDQMUQ3XA5CNFSM4IMTODJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAGN57I#issuecomment-537714429>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAN4NBESUWZ2NIUOCCDA74TQMUQ3XANCNFSM4IMTODJA>
.
|
PR checklist:
gh-pages
branch. Please refer to this PR). For new functionality, at least API.md should be updatednpm run perf
)Feel free to ask help with any of these boxes!
The above process doesn't apply to doc updates etc.