Replacing: StateObject:get() #217
dphfox
announced in
Announcements
Replies: 1 comment 2 replies
-
Is there any particular reason |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Starting with Fusion 0.3, you may see this error in your output.
Why is
:get()
problematic?In Fusion 0.2 and earlier,
:get()
would automatically decide which state object you were 'currently inside' by using some internal tracking magic. If you called:get()
inside of a computed's processor callback, for example, it'll connect to the state objects to detect and apply changes in the future:However, in other cases, such as when you're not currently inside any such callback,
:get()
is completely inert and only returns the value without adding dependencies, as it is assumed you don't need dependencies added in that situation:This automatic detection becomes brittle in deeply nested systems. Oftentimes,
:get()
will be called outside of any callbacks inside of a reusable function. This silently booby-traps that function, because if you ever call it from within a callback, it'll transitively apply the dependencies from those naked:get()
calls into your computed object (or other dependency-tracking state object):Needless to say, this is not acceptable. It should always be clear when your code can and can't add dependencies, so you can make strong assertations about how your code works without having to review or colour every function.
What's replacing it?
Firstly, a new function is being introduced to replace naked
:get()
calls outside of callbacks.Fusion.peek
takes a single value as an argument, and if it's a state object, it'll return the interior value:Importantly,
peek
will never add dependencies. The name is reflective of this - you are only 'peeking' at the value without enacting any other action. If you use it incorrectly, for example inside of a computed object, it's really obvious that it's wrong:For cases when you want your computed object (or equivalent) to listen to
foo
for changes, they'll now provide their own callback as an argument. This callback works identically topeek
, but will add any state objects as dependencies if it's given any. By convention, it's calleduse
:Why this?
The reason this is great is because it's now impossible for a nested function to add dependencies unless you give it the
use
callback:If you ever want to grant another function permission to add dependencies, you signal that permission by giving it the
use
callback, which now becomes a visible marker that this function has that permission:In addition to this, both functions accept constant (i.e. non-state) arguments. They're passed through directly:
This is especially powerful because it means you can freely mix and match constants and state objects in your code. This is particularly useful for defining flexible components, where properties can be either constants or state objects.
We hope that this change sets a much more stable and dependable foundation for your scripts into the future. Let us know if there's anything you find that's broken or unaccounted for :)
Beta Was this translation helpful? Give feedback.
All reactions