You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Observables created using fromResource seem to be impossible to use inside a computed from mainline MobX. Apart from creating derived values, this is also useful for simply wrapping the resulting observables in a familiar interface to be passed to functions that normally received an IComputedValue<T> rather than a {current(): T, dispose(): void, isAlive(): boolean}.
Even a simple computed: computed(() => fromResourceObservable.current()) throws an exception.
The exception thrown is an invariant violation of actions being used inside computed:
Uncaught Error: [mobx] Invariant failed: Computed values or transformers should not invoke actions or trigger other side effects
This happens because it appears that fromResource's sink uses an action in its implementation, here:
You can see an example of the problem here http://www.webpackbin.com/4k5kc14BM. I also made a zip of the project, since WebpackBin doesn't let me make the bin immutable.
In the above project, if you render the React component using obsUser directly, the component works fine. If you render it using computedUser, you trigger the above exception.
I ran into this problem myself trying to use MobX to wrap apollo-client as in your comment here. The reason being that Apollo's subscribe immediately calls sink on the stack of its first call, thus tripping this action guard.
All of these problems can be avoided as far as I can tell, by deferring the sink to occur on the 'next tick', so that it's outside the guards of the computed's execution, but this is an unreliable solution that should hopefully not be necessary.
The text was updated successfully, but these errors were encountered:
Observables created using
fromResource
seem to be impossible to use inside acomputed
from mainline MobX. Apart from creating derived values, this is also useful for simply wrapping the resulting observables in a familiar interface to be passed to functions that normally received anIComputedValue<T>
rather than a{current(): T, dispose(): void, isAlive(): boolean}
.Even a simple computed:
computed(() => fromResourceObservable.current())
throws an exception.The exception thrown is an invariant violation of actions being used inside
computed
:Uncaught Error: [mobx] Invariant failed: Computed values or transformers should not invoke actions or trigger other side effects
This happens because it appears that
fromResource
'ssink
uses an action in its implementation, here:mobx-utils/src/from-resource.ts
Line 95 in 9b0a137
Example
You can see an example of the problem here http://www.webpackbin.com/4k5kc14BM. I also made a zip of the project, since WebpackBin doesn't let me make the bin immutable.
In the above project, if you render the React component using
obsUser
directly, the component works fine. If you render it usingcomputedUser
, you trigger the above exception.I used exactly the code from the example by @mweststrate in the mobx-utils README with a mock
dbUserRecord
wrapped in acomputed
.I ran into this problem myself trying to use MobX to wrap
apollo-client
as in your comment here. The reason being that Apollo'ssubscribe
immediately callssink
on the stack of its first call, thus tripping this action guard.All of these problems can be avoided as far as I can tell, by deferring the
sink
to occur on the 'next tick', so that it's outside the guards of thecomputed
's execution, but this is an unreliable solution that should hopefully not be necessary.The text was updated successfully, but these errors were encountered: