mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 04:14:08 +00:00
feat: batch sync operations
This commit is contained in:
parent
eb127a05f1
commit
81f13eabc7
2 changed files with 36 additions and 6 deletions
|
|
@ -67,4 +67,29 @@ return function()
|
||||||
expect(calls[1]).to.equal(2)
|
expect(calls[1]).to.equal(2)
|
||||||
expect(calls[2]).to.equal(3)
|
expect(calls[2]).to.equal(3)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("batches patches", function()
|
||||||
|
local a = atom(0)
|
||||||
|
local b = atom(0)
|
||||||
|
local client = sync.client({ atoms = { a = a, b = b } })
|
||||||
|
local states = {}
|
||||||
|
|
||||||
|
subscribe(a, function()
|
||||||
|
states.b = b()
|
||||||
|
end)
|
||||||
|
|
||||||
|
subscribe(b, function()
|
||||||
|
states.a = a()
|
||||||
|
end)
|
||||||
|
|
||||||
|
client:sync({
|
||||||
|
type = "patch",
|
||||||
|
data = { a = 1, b = 2 },
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(a()).to.equal(1)
|
||||||
|
expect(b()).to.equal(2)
|
||||||
|
expect(states.a).to.equal(1)
|
||||||
|
expect(states.b).to.equal(2)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
local store = require(script.Parent.Parent.store)
|
||||||
local types = require(script.Parent.Parent.types)
|
local types = require(script.Parent.Parent.types)
|
||||||
type Atom<T> = types.Atom<T>
|
type Atom<T> = types.Atom<T>
|
||||||
type SyncPayload = types.SyncPayload
|
type SyncPayload = types.SyncPayload
|
||||||
|
|
@ -36,13 +37,17 @@ local function client(options: ClientOptions): ClientSyncer
|
||||||
for index = 1, select("#", ...) do
|
for index = 1, select("#", ...) do
|
||||||
local payload = select(index, ...)
|
local payload = select(index, ...)
|
||||||
|
|
||||||
for key, state in next, payload.data do
|
store.batch(function()
|
||||||
if payload.type == "patch" then
|
for key, state in next, payload.data do
|
||||||
atoms[key](patch.apply(atoms[key](), state))
|
local atom = atoms[key]
|
||||||
else
|
|
||||||
atoms[key](state)
|
if payload.type == "patch" then
|
||||||
|
atom(patch.apply(atom(), state))
|
||||||
|
else
|
||||||
|
atom(state)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue