mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51: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
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue