-
Notifications
You must be signed in to change notification settings - Fork 0
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
Make derived cells lazy #3
Comments
So for example, for the follow code: local c = cell(0)
local f1 = formula(function()
print(`f1 updated`)
return `f1: {c.value}`
end)
local f2 = formula(function()
print(`f2 updated`)
return `f2: {c.value}`
end)
local isone = formula(function()
return true
end)
subscribe(function()
if isone.value then
print(f1.value)
else
print(f2.value)
end
end) We want the following behavior: -- prints f1 updated
-- prints f1: 0
c.value = 10
-- prints f1 updated
-- prints f1: 10
c.value = 20
-- prints f1 updated
-- prints f1: 20
isone.value = false
-- prints f2 updated
-- prints f2: 20 However, currently the behavior is as follows -- prints f1 updated
-- prints f2 updated
-- prints f1: 0
c.value = 10
-- prints f1 updated
-- prints f2 updated
-- prints f1: 10
c.value = 20
-- prints f1 updated
-- prints f2 updated
-- prints f1: 20
isone.value = false
-- prints f1 updated
-- prints f2 updated
-- prints f2: 20 Clearly, formulas are calculated despite not being needed. As well, this means that merely CREATING a formula has the overhead of performing its calculation. Having it be deferred to its initial subscription would be better. |
This isn't necessarily a critical issue, as cells still works perfectly fine without it, and I don't particularly feel like working on adding this right now, but implementing this should be a simple addition that will just optimize any code using cells without requiring changes. |
You could also accomplish this behavior by just not using |
right now, deriving a formula from a cell will result in that calculation being made whenever the cell changes, even if nothing is subscribed to that formula. We should make it so formulas are lightweight and lazy, which is the behavior in the signals library cells is based off of: https://github.com/preactjs/signals
The text was updated successfully, but these errors were encountered: