mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 12:41:37 +00:00
fix: make toRaw track all calls
This commit is contained in:
parent
9b89284529
commit
189ae0f168
3 changed files with 21 additions and 14 deletions
|
|
@ -71,7 +71,7 @@ end)
|
||||||
|
|
||||||
test("should replicate partial state patches (reactive)", function()
|
test("should replicate partial state patches (reactive)", function()
|
||||||
local client = {}
|
local client = {}
|
||||||
local a: { [string]: number? } = Charm.reactive({ x = 0, y = 0, z = 0 })
|
local a = Charm.reactive({ x = { y = 0 }, z = 0 :: number? })
|
||||||
local target, payloads
|
local target, payloads
|
||||||
|
|
||||||
server.addSignalsToClient(client, { a = a })
|
server.addSignalsToClient(client, { a = a })
|
||||||
|
|
@ -82,18 +82,24 @@ test("should replicate partial state patches (reactive)", function()
|
||||||
server.flush()
|
server.flush()
|
||||||
assert(target == client, "Expected target to be the client")
|
assert(target == client, "Expected target to be the client")
|
||||||
assert(payloads[1].type == "init", "Expected first payload to be init")
|
assert(payloads[1].type == "init", "Expected first payload to be init")
|
||||||
assert(payloads[1].data.a.x == a.x, "Expected init payload to contain value 'a.x'")
|
assert(payloads[1].data.a.x ~= nil, "Expected init payload to contain value 'a.x'")
|
||||||
assert(payloads[1].data.a.y == a.y, "Expected init payload to contain value 'a.y'")
|
assert(payloads[1].data.a.x.y == a.x.y, "Expected init payload to contain value 'a.x.y'")
|
||||||
assert(payloads[1].data.a.z == a.z, "Expected init payload to contain value 'a.z'")
|
assert(payloads[1].data.a.z == a.z, "Expected init payload to contain value 'a.z'")
|
||||||
|
|
||||||
a.x = 1
|
a.x.y = 1
|
||||||
a.z = nil
|
a.z = nil
|
||||||
server.flush()
|
server.flush()
|
||||||
assert(target == client, "Expected target to be the client")
|
assert(target == client, "Expected target to be the client")
|
||||||
assert(payloads[1].type == "patch", "Expected first payload to be patch")
|
assert(payloads[1].type == "patch", "Expected first payload to be patch")
|
||||||
assert(payloads[1].data.a.x == 1, "Expected patch payload to contain value 'a.x'")
|
assert(payloads[1].data.a.x.y == 1, "Expected patch payload to contain value 'a.x'")
|
||||||
assert(payloads[1].data.a.y == nil, "Expected patch payload to not contain value 'a.y'")
|
|
||||||
assert(patch.isNone(payloads[1].data.a.z), "Expected patch payload to contain value 'a.z' as None")
|
assert(patch.isNone(payloads[1].data.a.z), "Expected patch payload to contain value 'a.z' as None")
|
||||||
|
|
||||||
|
a.z = 1
|
||||||
|
server.flush()
|
||||||
|
assert(target == client, "Expected target to be the client")
|
||||||
|
assert(payloads[1].type == "patch", "Expected first payload to be patch")
|
||||||
|
assert(payloads[1].data.a.z == 1, "Expected patch payload to contain value 'a.z'")
|
||||||
|
assert(payloads[1].data.a.x == nil, "Expected patch payload to not contain value 'a.x'")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test("should init new clients", function()
|
test("should init new clients", function()
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,10 @@ local function trackReactive<T>(key: string, proxy: ReactiveProxy<T>)
|
||||||
|
|
||||||
trackedState.dispose = Charm.effect(function()
|
trackedState.dispose = Charm.effect(function()
|
||||||
api.track()
|
api.track()
|
||||||
if not config.preserveHistory or firstRun then
|
if not config.preserveHistory then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if firstRun then
|
||||||
firstRun = false
|
firstRun = false
|
||||||
else
|
else
|
||||||
local oldSnapshot = snapshot
|
local oldSnapshot = snapshot
|
||||||
|
|
|
||||||
|
|
@ -933,16 +933,14 @@ end
|
||||||
]]
|
]]
|
||||||
local function toRaw<T>(value: T): T
|
local function toRaw<T>(value: T): T
|
||||||
if type(value) == "table" and getmetatable(value) ~= nil then
|
if type(value) == "table" and getmetatable(value) ~= nil then
|
||||||
local proxy: ReactiveProxy = value
|
local value: ReactiveProxy = value
|
||||||
local api = getmetatable(proxy)
|
local api = getmetatable(value)
|
||||||
if api.value == proxy.__target then
|
if api then
|
||||||
-- Track the root proxy since nested proxies are already tracked
|
|
||||||
api.track()
|
api.track()
|
||||||
|
return value.__target :: any
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return proxy.__target :: any
|
|
||||||
else
|
|
||||||
return value
|
return value
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local reactiveSystem = system.createReactiveSystem({
|
local reactiveSystem = system.createReactiveSystem({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue