Desired behaviour of Atom's become(Un)Observed handlers?
TL;DR: Are Atom's life cycle hooks more for synchronous or asynchronous actions. What are your use cases you wouldn't like to be beaked?
Atom main point is to expose hooks to becomeObserved and becomeUnobserved events.
I am rewriting bunch of mobx core (#452) and it brought me some questions about desired behavior of Atom.
Previously a is observed if there is a reaction/autorun deeply depending on a. Simply calling computed dependent on a wouldn't observe it.
Now it would change. For example during duration of a transaction all observables and computeds would be observed by transaction for it's duration (that's to recalculate computeds only when needed, even if not observed by reaction/autorun).
There are few different behaviors I could think of that make sense in different cases:
- Any Atom access trigger
becomeObserved.becomeUnobserved, when outside of any transactions/computations and no reaction/autorun depends on it. (so caled max once per transaction/...)- + If Atom is computing something expensive synchronously, it can cache data like parsing cookies/DOM queries. (benefits if not used in reaction/autorun, but called frequently in some action/transaction/computed)
- + simple and no significant performance difference
- - Some Atom's making asynchronous would make sure they cancel them if immediately unobserved
- (current)
becomeObservedwhen first reaction/autorun start depending on it.becomeUnobservedwhen last reaction/autorun stop depending on it.- + behavior doesn't change
- - more complex to implement.
- - performance disadvantage. wasteful if not depending on Atoms (using only observables/maps)
becomeObservedwhen outside of any transaction/computation, but reaction/autorun depends on it.becomeUnobserved- + if
becomeUnobservedis making only asynchronous requests it won't be called when not needed. - + simple, possibly most performant.
- - handler's shouldn't try doing any synchronous optimizations.
- + if
I personally only ever used Atom's for simple clock. What are other use cases? Also does anyone use those hooks to synchronously modify observable state? It would make behavior differences between my proposes solutions way bigger. It doesn't feel to me like a good think to do. Should we forbid/depreciate modifying state in those hooks?
I think the biggest use case for Atoms is to do subscribe to some (non mobx) observable, like a data base connector, that fires updates as along as we are interested in. I'm currently trying to capture that pattern here in a single utility: https://github.com/mobxjs/mobx-utils/blob/master/src/from-resource.ts. I think that is by far the most important case.
I think all proposed solutions cover that, but I think that the option 1) gives the clearest semantics: don't give the impression there is a distinction, just fire the hooks regardless why the atom was inspected. It's then up to the implementor to check whether that is a desirable situation (e.g. ok to un(subscribe) a lot, or to either optimize that or forbid usage outside reactions. (necessary api to check that should be exposed)
Hmmm being able to expose API for checking if observable is depended on by any reaction would be the equally complex to implementing option 2.
For creating Atom that would run only under reaction it could debounce observation:
class AtomDebounced extends Atom {
_debounce = null
constructor(name, onBecomeObservedHandler, onBecomeUnobservedHandler) {
super.constructor(
name,
() => {
if(this._debounce === null)
this._debounce = setImmediate(() => {
this._debounce = null
onBecomeObservedHandler()
})
},
() => {
if(this._debounce === null)
onBecomeUnobservedHandler()
else
clearImmediate(this._debounce)
}
)
}
}
it would make onBecomeObservedHandler be called always outside of batch, hence Atom is kept alive by reaction
note that this solution based on the same the idea as 3., with the difference that It would hook to end of the batch instead of timeout
EDIT: add note
Sorry I meant a static function that returns true if a reaction is currently running. Then people can make the choice to ignore or forbid onBecomeObserve doing something outside of a tracked function.
Op do 11 aug. 2016 om 21:27 schreef Albert [email protected]:
Hmmm being able to expose API for checking if observable is depended on by any reaction would be the equally complex to implementing option 2.
For creating Atom that would run only under reaction it could debounce observation:
class AtomDebounced extends Atom { _debounce = null constructor(name, onBecomeObservedHandler, onBecomeUnobservedHandler) { super.constructor( name, () => { if(this._debounce === null) this._debounce = setImmediate(() => { this._debounce = null onBecomeObservedHandler() }) }, () => { if(this._debounce === null) onBecomeUnobservedHandler() else clearImmediate(this._debounce) } ) } }
it would make onBecomeObservedHandler be called always outside of batch, hence Atom is kept alive by reaction
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/mobxjs/mobx/issues/477#issuecomment-239265451, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvGhC1EZpDWLoEQ9sfVV05vTrvseYZ-ks5qe3e6gaJpZM4JiVk9 .
@mweststrate This maybe could be useful in many cases, but the possible scenario then is:
const a = new Atom(...)
const b = computed(() => a.get())
let dispatch = null
mobx.transaction(() => {
b.get();
dispatch = autorun(() => b.get())
})
...
dispatch()
autorun a is being accessed only on first b access (b then remembers result)
It will never be accessed during reaction, but reaction depends on it.
heh nice one. so maybe we should not provide this distinction and just apply strategy one. I want to try work towards a merge and release next days (with a beta first). I'll gonna merge this into a separate branch and give you access to the repo that should make collaboration easier
Then I'll quickly finish up implementing first solution and update PR before you merge
I think this issue can be closed now?