Breaking changes to Computed object update timings #322
dphfox
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Right now, some users of Fusion treat
Computed
objects as observers, placing non-computation code inside of them.This produces the expected output.
However, there are two possible changes on the horizon that will break this use case.
Dissolving state objects
As per #301, Computed objects that do not
use()
any state objects may be changed to return a constant value rather than a state object. This involves running the computation before theComputed
is constructed, with a specialuse()
function that only returns constants, and errors for state.This means it's possible (and expected) that a
Computed
object might only partially execute, causing a duplicate print in our example:This isn't certain to go through yet - it'll need a bit more testing to definitively say whether it's something we want to go ahead with or not - but if it does go through, then it may cause less predictable behaviour if you depend on non-computational code in Computed objects.
Execution model changes
Fusion is moving to a hybrid push-pull execution model, which means that (aside from the above test for constant-ness) computations won't run until you ask for their result.
To better demonstrate this, here's an idiomatic example.
This change is designed to not only reduce the amount of computation work done unnecessarily, but also to improve correctness - it helps us guarantee that objects you access are updated in the right order.
The solution
These changes are the reason why we have always advised - and continue to advise - the use of
Observer
objects instead ofComputed
objects whenever you care about the execution of the code, rather than only its result.Even with the above two changes being applied, you will notice this observer example remains functional.
This produces the desired output again.
There are efforts to improve observers right now, over in #258 and #310, so that it is easier to use them in place of
Computed
objects.See you there :)
Beta Was this translation helpful? Give feedback.
All reactions