From 2fc3be34d0256fd0d6d57895930ce25175d9fe67 Mon Sep 17 00:00:00 2001 From: littensy <56808540+littensy@users.noreply.github.com> Date: Wed, 30 Sep 2026 19:20:51 -0700 Subject: [PATCH] fix(charm-sync): preserve global patch list order for nil patches Resolves #67 --- packages/charm-sync/src/server.luau | 6 +++--- packages/charm-sync/test/server.test.luau | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/charm-sync/src/server.luau b/packages/charm-sync/src/server.luau index 0494804..6485e18 100644 --- a/packages/charm-sync/src/server.luau +++ b/packages/charm-sync/src/server.luau @@ -125,8 +125,8 @@ local function diffGlobalUpdateBuffer(): { any } local patchCount = #globalUpdates local patches = table.create(patchCount) - for _, update in globalUpdates do - table.insert(patches, patch.diff(update.oldValue, update.newValue)) + for index, update in globalUpdates do + patches[index] = patch.diff(update.oldValue, update.newValue) end return patches @@ -146,7 +146,7 @@ local function selectFromGlobalPatchList(client: any, globalPatches: { any }): { for index, update in globalUpdates do local patchData = globalPatches[index] - if not patchData then + if patchData == nil then continue end diff --git a/packages/charm-sync/test/server.test.luau b/packages/charm-sync/test/server.test.luau index d43e7b8..d24970c 100644 --- a/packages/charm-sync/test/server.test.luau +++ b/packages/charm-sync/test/server.test.luau @@ -403,6 +403,27 @@ test.describe("historical", function() server.flush() assert(#payloads == 0, "Expected no payloads to be sent for unchanged state") end) + + test("should maintain patch list order for stale update", function() + local a = Charm.atom({ x = 1 }) + local b = Charm.atom({ y = 1 }) + local payloads + + server.connect(function(_client, _payloads) + payloads = _payloads + end) + + server.addSignalsToClient("client", { a = a, b = b }) + server.flush() + + payloads = nil + a({ x = 1 }) -- new table, same contents: diff is nil + b({ y = 2 }) + server.flush() + assert(payloads, "Expected payloads to be sent") + assert(payloads[1].type == "patch", "Expected payload to be patch") + assert(payloads[1].data.b.y == 2, "Expected payload to contain atom 'b'") + end) end) test("addSignalsToClient should ignore tracking context", function()