refactor: iterate over previous data in mapped

This commit is contained in:
littensy 2025-05-11 17:49:01 -07:00
parent 546df8cabe
commit 210e948225
2 changed files with 9 additions and 4 deletions

View file

@ -32,14 +32,18 @@ local function mapped<KI, VI, KO, VO>(selector: () -> { [KI]: VI }, mapper: (ite
local previousData = currentData
currentData = selector()
for key, cachedKey in keyCache do
for key in previousData do
if currentData[key] ~= nil then
continue
end
currentOutput = ensureCanMutateOutput(currentOutput, previousOutput)
currentOutput[cachedKey] = nil
keyCache[key] = nil
local cachedKey = keyCache[key]
if cachedKey then
currentOutput = ensureCanMutateOutput(currentOutput, previousOutput)
currentOutput[cachedKey] = nil
keyCache[key] = nil
end
end
for key, value in currentData do

View file

@ -52,6 +52,7 @@ test("should not update if no values change", function()
src({ 4, 5, 6, 7 })
src({ 1, 2, 3 })
map()
assert(calls == 3, "should not map unchanged values")
assert(table.concat(map()) == "234", table.concat(map()))
assert(prev == map(), "should not create new table")