-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use case for Signal.subtle.{un,}watched
options for Signal.Computed
?
#172
Comments
Do you mean, as opposed to just having it for state? I think @shaylew and @modderme123 had some thoughts about how it could relate to async signals. |
@littledan Yes, that was the idea. |
There are definitely async-related reasons to want these on States, but if there was an async-related reason we needed them on Computeds I'm blanking on that one. (It's possible @modderme123 had one?) The case that comes to mind for me is for caching of computeds for derived state, where you want to get sharing where possible but don't want to leak computeds that could otherwise be collected. For example, if you have a map where equality comparison of values is potentially expensive, you might want something like this to share the equality comparison work by sharing the selector computeds: // (I neither ran nor typechecked this code but I think it's coherent?)
function selectFrom<K, V>(map: Signal<Map<K, V>>, options: ComputedOptions = {}): (k: K) => Computed<V> {
const table = new Map<K, Computed<V>>()
return (k: K) => {
return table.get(k) ?? new Signal.Computed(() => map.get().get(k), {
...options,
[Signal.subtle.watched](): { table.set(k, this) },
[Signal.subtle.unwatched](): { table.delete(k) }
})
}
} There are likely other ways to accomplish this, and yeah the "use a dummy State" construction is sufficient in theory, it just may be unergonomic in practice. So this is more of a "seems like it's probably a good idea" argument than a "this is strictly necessary" one. |
@shaylew Wouldn't it be easier to just, in the parent Like instead of this: const getKey = selectFrom(someMap)
const selected = getKey("foo")
const useSelected = new Signal.Computed(() => {
doThings(selected.get())
}) Just doing this: const useSelected = new Signal.Computed(() => {
const selected = someMap.get().get("foo")
doThings(selected)
}) |
@dead-claudia That makes |
@shaylew Fair point. What about this? const selected = new Signal.Computed(() => someMap.get().get("foo"))
const useSelected = new Signal.Computed(() => {
doThings(selected.get())
}) Just trying to see the point of caching a computed here that's simple enough to where the perf difference is meaningful. |
Yeah, that's the same idea as where I was going and where Solid tries to go with In cases like these the value is actually all in memoizing/sharing the Stuff like this |
@shaylew Ah, thank you! I'll go ahead and close this since my question here was answered. |
I'm struggling to come up with one. It also seems redundant, since you can just do this:
The text was updated successfully, but these errors were encountered: