mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
fix: apply patches to non-table values
This commit is contained in:
parent
382bc3a904
commit
f2ec300204
1 changed files with 8 additions and 10 deletions
|
|
@ -40,19 +40,17 @@ local function diff(prevState: { [any]: any }, nextState: { [any]: any })
|
||||||
return patches
|
return patches
|
||||||
end
|
end
|
||||||
|
|
||||||
local function apply(state: { [any]: any }, patches: { [any]: any })
|
local function apply(state: any, patches: any): any
|
||||||
|
if type(patches) == "table" and patches.__none == "__none" then
|
||||||
|
return nil
|
||||||
|
elseif type(state) ~= "table" or type(patches) ~= "table" then
|
||||||
|
return patches
|
||||||
|
end
|
||||||
|
|
||||||
local nextState = table.clone(state)
|
local nextState = table.clone(state)
|
||||||
|
|
||||||
for key, patch in patches do
|
for key, patch in patches do
|
||||||
local current = nextState[key]
|
nextState[key] = apply(nextState[key], patch)
|
||||||
|
|
||||||
if type(patch) == "table" and patch.__none == "__none" then
|
|
||||||
nextState[key] = nil
|
|
||||||
elseif type(patch) == "table" and type(current) == "table" then
|
|
||||||
nextState[key] = apply(current, patch)
|
|
||||||
else
|
|
||||||
nextState[key] = patch
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return nextState
|
return nextState
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue