Fix edge case with map functions

This commit is contained in:
aaron 2024-11-27 19:43:51 +00:00
parent 44fe65ee5e
commit e60aa57ca2
2 changed files with 52 additions and 29 deletions

View file

@ -61,6 +61,8 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
local cv = input_cache[i] local cv = input_cache[i]
if cv ~= v then if cv ~= v then
input_cache[i] = v
if cv == nil then -- create new scope and run transform if cv == nil then -- create new scope and run transform
local scope = create_node(subowner, false, false) local scope = create_node(subowner, false, false)
scopes[i] = scope :: Node<any> scopes[i] = scope :: Node<any>
@ -87,8 +89,6 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
input_nodes[i].cache = v input_nodes[i].cache = v
update_descendants(input_nodes[i]) update_descendants(input_nodes[i])
end end
input_cache[i] = v
end end
end end

View file

@ -2646,10 +2646,12 @@ TEST("strict", wrap_root(function()
do CASE "destruction of active scope" do CASE "destruction of active scope"
local src = source(false) local src = source(false)
local count = 0
root(function() root(function()
show(src, function() show(src, function()
src(false) src(false)
vide.cleanup(function() count += 1 end)
return {} return {}
end) end)
end) end)
@ -2661,38 +2663,59 @@ TEST("strict", wrap_root(function()
CHECK(not ok) CHECK(not ok)
end end
-- todo: review intended behavior here do CASE "destruction of active scope in indexes"
-- do CASE "destruction of active scope in indexes" local src = source {}
-- local src = source {}
-- local tmp local count_1 = 0
-- root(function() local count_2 = 0
-- effect(function()
-- untrack(function()
-- tmp = indexes(src, function()
-- vide.cleanup(function() print "test" end)
-- src {}
-- print "updated"
-- vide.cleanup(function() print "test2" end)
-- print "end"
-- return {}
-- end)
-- return nil
-- end)
-- end)
-- end)
-- print "setting" root(function()
effect(function()
untrack(function()
indexes(src, function()
vide.cleanup(function() count_1 += 1 end)
src {}
vide.cleanup(function() count_2 += 1 end)
return {}
end)
return nil
end)
end)
end)
-- local ok = pcall(function() local ok = pcall(function()
-- src { 1 } src { 1 }
-- print "done" end)
-- end)
-- print(#tmp()) CHECK(not ok)
end
-- CHECK(not ok) do CASE "destruction of active scope in values"
-- end local src = source {}
local count_1 = 0
local count_2 = 0
root(function()
effect(function()
untrack(function()
values(src, function()
vide.cleanup(function() count_1 += 1 end)
src {}
vide.cleanup(function() count_2 += 1 end)
return {}
end)
return nil
end)
end)
end)
local ok = pcall(function()
src { {} }
end)
CHECK(not ok)
end
end)) end))
local ok = FINISH() local ok = FINISH()