This commit is contained in:
aaron 2023-09-13 21:14:55 +01:00
parent 7dead44ac5
commit a425c6b8be
3 changed files with 57 additions and 18 deletions

View file

@ -109,16 +109,12 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
return output_array
end
local output = create_node(false :: any)
output.effect = function()
local output = create_node(false :: any, function()
return update_children(input())
end
end)
open_scope(output)
evaluate_node(output)
output.cache = update_children(input())
close_scope()
return function()
track(output)
@ -126,7 +122,6 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
end
end
-- todo: optimize output array
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
local owner = get_scope()
if not owner then
@ -211,16 +206,11 @@ local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () ->
return output_array
end
local output = create_node(false :: any)
output.effect = function()
local output = create_node(false :: any, function()
return update_children(input())
end
end)
open_scope(output)
output.cache = update_children(input())
close_scope()
evaluate_node(output)
return function()
track(output)

View file

@ -73,7 +73,7 @@ BENCH("set derived value", function()
local src = vide.source(1)
vide.root(function()
local _derived = vide.derive(src)
local _derived = vide.derive(function() return src() end)
for i = 1, START(N) do
src(i)

View file

@ -399,7 +399,7 @@ TEST("derive()", wrap_root(function()
local count = 0
effect(function() c() count += 1 end)
effect(function() c(); count += 1 end)
b(true)
CHECK(c() == "b")
@ -453,11 +453,28 @@ TEST("derive()", wrap_root(function()
gc()
CHECK(wref[1])
end
do CASE "raw derive"
local a = source(1)
local b = derive(a)
local count = 0
effect(function()
b()
count += 1
end)
CHECK(count == 1)
a(2)
CHECK(count == 2)
end
end))
TEST("effect()", wrap_root(function()
local source = vide.source
local effect = vide.effect
local derive = vide.derive
do CASE "rerun on source change"
local a = source(1)
@ -476,6 +493,38 @@ TEST("effect()", wrap_root(function()
b(2)
CHECK(count == 3)
end
do CASE "rerun on derived source change"
local num = source(0)
local text = derive(function() return tostring(num()) end)
local count = 0
effect(function()
text()
count += 1
end)
num(1)
CHECK(count == 2)
end
do CASE "cache"
local num = source(0)
local count
effect(function(x: number)
num()
count = x + 1
return x + 1
end, 0)
num(1)
CHECK(count == 2)
end
end))
TEST("cleanup()", wrap_root(function()