Repository navigation
Get/reset overridden $derived values
#18926
sevmonster
started this conversation in
Ideas
Replies: 1 comment 1 reply
|
For things like this, no new rune should be strictly necessary. I would recommend looking into encapsulating the logic in an object or class with interal The object could then e.g. expose a |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Now that component
$propsand$derivedvalues can be temporarily overridden locally, there seems to be a bit of an issue as far as I can see: there's no way to get the original value, until the computed value changes and clears out the override.There are two facets to this: figuring out the original value of the variable, and resetting it back to its original value.
Getting the original value
From a holistic standpoint, the answer to this one is obvious: just create a second variable that you can use as a buffer, and reference the original variable when you need the original value. And if you want to reset the value back to its original, you can simply override the second variable with the first one's value.
This seems like it would work fine, but has multiple problems:
$deriveddirectly instead of making a second value, sure. But this becomes difficult if not impossible when the source becomes complicated (e.g.$derived(somevalue ?? someothervalue ?? myapp.value2 ?? ...)) or is part of a proxy object that is handed down to you without knowing the real source. It's more obvious when working with components and packages you do not own.Restoring/recalculating the value
$derivedvalues recalculate when their tracked dependencies change. This can be abused to force the value to recalculate itself, even if the output doesn't actually change. I don't like this though, it feels a bit hacky and is pretty ugly. I could also see it being unclear to new users about what it's supposed to be doing.It would be good to have a more idiomatic way of getting original values and resetting overrides. Though, I don't have any suggestions other than potentially introducing new runes like
$original()or$reset(), which as I understand it is not something the team is particularly enthused about.To play the devil's advocate:
This, of course, does introduce its own layer of fuzz: the reader might not know the value can reset, unless they see the part that explicitly resets it.
All reactions