diff --git a/packages/charm-sync/src/__tests__/patch.spec.luau b/packages/charm-sync/src/__tests__/patch.spec.luau index cad535f..7ce89c2 100644 --- a/packages/charm-sync/src/__tests__/patch.spec.luau +++ b/packages/charm-sync/src/__tests__/patch.spec.luau @@ -127,4 +127,10 @@ test("patches mutable state", function() assert(targetState.qaz == "qux", "should add the new value") end) +test("removes table from mutable state", function() + local targetState = { foo = { bar = "baz" } } + patch.applyMutable(targetState, { foo = { __none = "__none" } }) + assert(targetState.foo == nil, "should remove the table") +end) + return {} diff --git a/packages/charm-sync/src/patch.luau b/packages/charm-sync/src/patch.luau index 3892e9a..0d1684d 100644 --- a/packages/charm-sync/src/patch.luau +++ b/packages/charm-sync/src/patch.luau @@ -112,22 +112,22 @@ local function apply(oldState: any, diffs: any): any end local function applyMutable(targetState: any, sourceState: any): any - if type(targetState) == "table" and type(sourceState) == "table" then - local targetIsArray = targetState[1] ~= nil - - for key, value in sourceState do - if targetIsArray and type(key) == "string" then - key = tonumber(key) or key - end - targetState[key] = applyMutable(targetState[key], value) - end - - return targetState - elseif isNone(sourceState) then + if isNone(sourceState) then return nil - else + elseif type(targetState) ~= "table" or type(sourceState) ~= "table" then return sourceState end + + local targetIsArray = targetState[1] ~= nil + + for key, value in sourceState do + if targetIsArray and type(key) == "string" then + key = tonumber(key) or key + end + targetState[key] = applyMutable(targetState[key], value) + end + + return targetState end return {