mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Update indexes source for table updates
This commit is contained in:
parent
2518e292ed
commit
a8739229fc
2 changed files with 40 additions and 1 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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 }
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue