mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 11:44:08 +00:00
feat: finalize sync docs
This commit is contained in:
parent
37f79ecc3f
commit
ea40833d3b
15 changed files with 400 additions and 206 deletions
|
|
@ -6,13 +6,13 @@ local test = suite.test
|
|||
local afterEach = suite.afterEach
|
||||
|
||||
afterEach(function()
|
||||
client.reset()
|
||||
client.removeAllSignals()
|
||||
end)
|
||||
|
||||
test("should hydrate atom", function()
|
||||
local a = Charm.atom(1)
|
||||
|
||||
client.register({ a = a })
|
||||
client.addSignals({ a = a })
|
||||
client.hydrate({
|
||||
{ type = "init", data = { a = 2 } },
|
||||
})
|
||||
|
|
@ -24,7 +24,7 @@ test("should merge patch", function()
|
|||
local a = Charm.atom({ b = 1, c = 2 })
|
||||
local previous = a()
|
||||
|
||||
client.register({ a = a })
|
||||
client.addSignals({ a = a })
|
||||
client.hydrate({
|
||||
{ type = "init", data = { a = a() } },
|
||||
})
|
||||
|
|
@ -43,7 +43,7 @@ end)
|
|||
test("should fix sparse array patches", function()
|
||||
local a = Charm.atom({ 1 })
|
||||
|
||||
client.register({ a = a })
|
||||
client.addSignals({ a = a })
|
||||
client.hydrate({
|
||||
{ type = "init", data = { a = a() } },
|
||||
})
|
||||
|
|
@ -63,7 +63,7 @@ test("should merge multiple patches", function()
|
|||
local a = Charm.atom(1)
|
||||
local calls = {}
|
||||
|
||||
client.register({ a = a })
|
||||
client.addSignals({ a = a })
|
||||
|
||||
Charm.subscribe(a, function(value)
|
||||
table.insert(calls, value)
|
||||
|
|
@ -87,7 +87,7 @@ test("should batch patches", function()
|
|||
local b = Charm.atom(0)
|
||||
local states = {}
|
||||
|
||||
client.register({ a = a, b = b })
|
||||
client.addSignals({ a = a, b = b })
|
||||
|
||||
Charm.subscribe(a, function()
|
||||
states.b = b()
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ local test = suite.test
|
|||
|
||||
afterEach(function()
|
||||
server.reset()
|
||||
client.reset()
|
||||
client.removeAllSignals()
|
||||
config.preserveHistory = false
|
||||
end)
|
||||
|
||||
|
|
@ -20,8 +20,8 @@ test("should replicate server state to client", function()
|
|||
local clientA = Charm.atom({ count = -1 })
|
||||
local clientB = Charm.atom({ count = -1 })
|
||||
|
||||
client.register({ a = clientA, b = clientB })
|
||||
server.subscribeClient(target, { a = serverA, b = serverB })
|
||||
client.addSignals({ a = clientA, b = clientB })
|
||||
server.addSignalsToClient(target, { a = serverA, b = serverB })
|
||||
server.connect(function(syncTarget, payloads)
|
||||
if syncTarget == target then
|
||||
client.hydrate(payloads)
|
||||
|
|
@ -54,8 +54,8 @@ test("should replicate historical server state to client", function()
|
|||
|
||||
config.preserveHistory = true
|
||||
|
||||
client.register({ a = clientA, b = clientB })
|
||||
server.subscribeClient(target, { a = serverA, b = serverB })
|
||||
client.addSignals({ a = clientA, b = clientB })
|
||||
server.addSignalsToClient(target, { a = serverA, b = serverB })
|
||||
server.connect(function(syncTarget, payloads)
|
||||
if syncTarget == target then
|
||||
client.hydrate(payloads)
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ test("should replicate state changes", function()
|
|||
local c = Charm.atom(0)
|
||||
local target, payloads
|
||||
|
||||
server.subscribeClient(client, { a = a, b = b, c = c })
|
||||
server.addSignalsToClient(client, { a = a, b = b, c = c })
|
||||
server.connect(function(...)
|
||||
target, payloads = ...
|
||||
end)
|
||||
|
|
@ -47,7 +47,7 @@ test("should replicate partial state patches", function()
|
|||
local b = Charm.atom(0 :: number?)
|
||||
local target, payloads
|
||||
|
||||
server.subscribeClient(client, { a = a, b = b })
|
||||
server.addSignalsToClient(client, { a = a, b = b })
|
||||
server.connect(function(...)
|
||||
target, payloads = ...
|
||||
end)
|
||||
|
|
@ -75,7 +75,7 @@ test("should init new clients", function()
|
|||
local a = Charm.atom(0)
|
||||
local payloadsA, payloadsB
|
||||
|
||||
server.subscribeClient(clientA, { a = a })
|
||||
server.addSignalsToClient(clientA, { a = a })
|
||||
server.connect(function(target, payloads)
|
||||
if target == clientA then
|
||||
payloadsA = payloads
|
||||
|
|
@ -89,7 +89,7 @@ test("should init new clients", function()
|
|||
assert(payloadsA[1].type == "init", "Expected first payload to be init for client A")
|
||||
assert(payloadsA[1].data.a == a(), "Expected init payload to contain atom 'a' for client A")
|
||||
|
||||
server.subscribeClient(clientB, { a = a })
|
||||
server.addSignalsToClient(clientB, { a = a })
|
||||
a(2)
|
||||
server.flush()
|
||||
assert(payloadsA[1].type == "patch", "Expected first payload to be patch for client A")
|
||||
|
|
@ -104,13 +104,13 @@ test("should init new atoms", function()
|
|||
local b = Charm.atom(0)
|
||||
local target, payloads
|
||||
|
||||
server.subscribeClient(client, { a = a })
|
||||
server.addSignalsToClient(client, { a = a })
|
||||
server.connect(function(...)
|
||||
target, payloads = ...
|
||||
end)
|
||||
|
||||
server.flush()
|
||||
server.subscribeClient(client, { b = b })
|
||||
server.addSignalsToClient(client, { b = b })
|
||||
a(1)
|
||||
b(1)
|
||||
server.flush()
|
||||
|
|
@ -129,8 +129,8 @@ test("should only replicate to synced clients", function()
|
|||
local b = Charm.atom(0)
|
||||
local payloadsA, payloadsB
|
||||
|
||||
server.subscribeClient(clientA, { a = a })
|
||||
server.subscribeClient(clientB, { b = b })
|
||||
server.addSignalsToClient(clientA, { a = a })
|
||||
server.addSignalsToClient(clientB, { b = b })
|
||||
server.connect(function(target, payloads)
|
||||
if target == clientA then
|
||||
payloadsA = payloads
|
||||
|
|
@ -170,7 +170,7 @@ test("should validate state", function()
|
|||
local client = {}
|
||||
local a = Charm.atom({})
|
||||
|
||||
server.subscribeClient(client, { a = a })
|
||||
server.addSignalsToClient(client, { a = a })
|
||||
server.connect(function() end)
|
||||
|
||||
a({ [0.5] = true })
|
||||
|
|
@ -185,7 +185,7 @@ test("should serialize state", function()
|
|||
local a = Charm.atom({ 1, 2, 3, 4, 5 })
|
||||
local payloads
|
||||
|
||||
server.subscribeClient(client, { a = a })
|
||||
server.addSignalsToClient(client, { a = a })
|
||||
server.connect(function(_, p)
|
||||
payloads = p
|
||||
end)
|
||||
|
|
@ -213,7 +213,7 @@ describe("historical", function()
|
|||
local c = Charm.atom(0)
|
||||
local target, payloads
|
||||
|
||||
server.subscribeClient(client, { a = a, b = b, c = c })
|
||||
server.addSignalsToClient(client, { a = a, b = b, c = c })
|
||||
server.connect(function(...)
|
||||
target, payloads = ...
|
||||
end)
|
||||
|
|
@ -242,7 +242,7 @@ describe("historical", function()
|
|||
local a = Charm.atom(0)
|
||||
local payloadsA, payloadsB
|
||||
|
||||
server.subscribeClient(clientA, { a = a })
|
||||
server.addSignalsToClient(clientA, { a = a })
|
||||
server.connect(function(target, payloads)
|
||||
if target == clientA then
|
||||
payloadsA = payloads
|
||||
|
|
@ -259,7 +259,7 @@ describe("historical", function()
|
|||
assert(payloadsA[2].type == "patch", "Expected second payload to be patch for client A")
|
||||
assert(payloadsA[2].data.a == 2, "Expected second patch payload to contain atom 'a' with value 1 for client A")
|
||||
|
||||
server.subscribeClient(clientB, { a = a })
|
||||
server.addSignalsToClient(clientB, { a = a })
|
||||
a(3)
|
||||
a(4)
|
||||
server.flush()
|
||||
|
|
@ -278,7 +278,7 @@ describe("historical", function()
|
|||
local b = Charm.atom(0)
|
||||
local payloads
|
||||
|
||||
server.subscribeClient(client, { a = a })
|
||||
server.addSignalsToClient(client, { a = a })
|
||||
server.connect(function(_, ...)
|
||||
payloads = ...
|
||||
end)
|
||||
|
|
@ -287,7 +287,7 @@ describe("historical", function()
|
|||
assert(payloads[1].type == "init", "Expected first payload to be init")
|
||||
assert(payloads[1].data.a == 0, "Expected init payload to contain atom 'a'")
|
||||
|
||||
server.subscribeClient(client, { b = b })
|
||||
server.addSignalsToClient(client, { b = b })
|
||||
a(1)
|
||||
a(2)
|
||||
b(1)
|
||||
|
|
|
|||
|
|
@ -2,17 +2,15 @@ local Charm = require("../../Charm")
|
|||
local suite = require("../../../../test/suite")
|
||||
local utils = require("../utils")
|
||||
|
||||
local atom = Charm.atom
|
||||
local test = suite.test
|
||||
|
||||
test("should flatten atoms", function()
|
||||
local getF, setF = Charm.signal(0)
|
||||
test("should flatten functions", function()
|
||||
local input = {
|
||||
a = atom(0),
|
||||
b = { c = atom(0) },
|
||||
d = { e = { f = utils.signalToAtom(getF, setF) } },
|
||||
a = Charm.atom(0),
|
||||
b = { c = Charm.atom(0) },
|
||||
d = { e = { f = utils.signalToAtom(Charm.signal(0)) } },
|
||||
}
|
||||
local result = utils.flattenState(input)
|
||||
local result = utils.flatten(input)
|
||||
|
||||
assert(result.a == input.a, "should flatten the first level")
|
||||
assert(result["b/c"] == input.b.c, "should flatten the second level")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue