mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 12:41:37 +00:00
refactor: iterate over previous data in mapped
This commit is contained in:
parent
546df8cabe
commit
210e948225
2 changed files with 9 additions and 4 deletions
|
|
@ -32,14 +32,18 @@ local function mapped<KI, VI, KO, VO>(selector: () -> { [KI]: VI }, mapper: (ite
|
||||||
local previousData = currentData
|
local previousData = currentData
|
||||||
currentData = selector()
|
currentData = selector()
|
||||||
|
|
||||||
for key, cachedKey in keyCache do
|
for key in previousData do
|
||||||
if currentData[key] ~= nil then
|
if currentData[key] ~= nil then
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
|
|
||||||
currentOutput = ensureCanMutateOutput(currentOutput, previousOutput)
|
local cachedKey = keyCache[key]
|
||||||
currentOutput[cachedKey] = nil
|
|
||||||
keyCache[key] = nil
|
if cachedKey then
|
||||||
|
currentOutput = ensureCanMutateOutput(currentOutput, previousOutput)
|
||||||
|
currentOutput[cachedKey] = nil
|
||||||
|
keyCache[key] = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for key, value in currentData do
|
for key, value in currentData do
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@ test("should not update if no values change", function()
|
||||||
|
|
||||||
src({ 4, 5, 6, 7 })
|
src({ 4, 5, 6, 7 })
|
||||||
src({ 1, 2, 3 })
|
src({ 1, 2, 3 })
|
||||||
|
map()
|
||||||
assert(calls == 3, "should not map unchanged values")
|
assert(calls == 3, "should not map unchanged values")
|
||||||
assert(table.concat(map()) == "234", table.concat(map()))
|
assert(table.concat(map()) == "234", table.concat(map()))
|
||||||
assert(prev == map(), "should not create new table")
|
assert(prev == map(), "should not create new table")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue