dphfox/Fusion

Computed does not capture dependency when Value created inside the callback

kezzyhko opened this issue · 2 comments

The problem

The problem I have is values that created dynamically. I have a setup where values are created on the first request by some unique name. If the first time value accessed is inside Computed's callback, the dependency is not captured and when you update the value, the computed's value is not recalculated.

The workaround

As the workaround it is possible to create all values when initializing the values array. However, for that the code needs to know all possible values' names, which is not really convenient.

Code to reproduce


local Fusion = require(game:GetService("ReplicatedStorage"):WaitForChild("Fusion"))

local values = {}
local function getValue(name : string) : Fusion.Value<number>
    if not values[name] then
        values[name] = Fusion.Value(10)
    end
    return values[name]
end

local computed = Fusion.Computed(function()
	return getValue("test"):get()
end)

print(computed:get()) -- prints "10", as expected

getValue("test"):set(20)

print(computed:get()) -- prints "10", not "20"

Seems like the same root cause as #270 ?

dphfox commented

This is both intentional and also removed in 0.3 - the automatic dependency manager in 0.2 explicitly rejected dependencies made inside of the Computed itself to avoid cycles. Since the automatic dependency manager no longer exists in 0.3, this issue is already resolved.