Update indexes source for table updates

This commit is contained in:
daimond113 2025-08-29 21:54:45 +02:00
parent 2518e292ed
commit a8739229fc
No known key found for this signature in database
2 changed files with 40 additions and 1 deletions

View file

@ -44,7 +44,7 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
for i, v in data do for i, v in data do
local cv = input_cache[i] local cv = input_cache[i]
if cv ~= v then if cv ~= v or (type(v) == "table" and not table.isfrozen(v)) then
input_cache[i] = v input_cache[i] = v
if cv == nil then -- create new scope and run transform if cv == nil then -- create new scope and run transform

View file

@ -1452,6 +1452,45 @@ TEST("indexes()", wrap_root(function()
CHECK(count[3] == 1) CHECK(count[3] == 1)
end end
do CASE "table caching"
local mutable_table = {}
local frozen_table = table.freeze { 4, 5, 6 }
local input = source { mutable_table, frozen_table }
local count = table.create(2, 0)
local _, output = vide.root(function()
local output = indexes(input, function(v, i)
effect(function()
v()
count[i] += 1
end)
return v
end)
return output
end)
CHECK(count[1] == 1)
CHECK(count[2] == 1)
CHECK(#output()[1]() == 0)
CHECK(#output()[2]() == 3)
mutable_table[1] = 1
mutable_table[2] = 2
mutable_table[3] = 3
input { mutable_table, frozen_table }
CHECK(count[1] == 2)
CHECK(count[2] == 1)
CHECK(#output()[1]() == 3)
CHECK(#output()[2]() == 3)
end
do CASE "removal reflected" do CASE "removal reflected"
local input = source { 1, 2, 3 } local input = source { 1, 2, 3 }