diff --git a/src/indexes.luau b/src/indexes.luau index acad4c0..74d6756 100644 --- a/src/indexes.luau +++ b/src/indexes.luau @@ -44,7 +44,7 @@ local function indexes(input: () -> Map, transform: (() -> VI, for i, v in data do 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 if cv == nil then -- create new scope and run transform diff --git a/test/tests.luau b/test/tests.luau index 254e955..1a1687d 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -1452,6 +1452,45 @@ TEST("indexes()", wrap_root(function() CHECK(count[3] == 1) 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" local input = source { 1, 2, 3 }