feat: batch sync operations

This commit is contained in:
Richard 2024-08-18 14:43:29 -07:00
parent eb127a05f1
commit 81f13eabc7
2 changed files with 36 additions and 6 deletions

View file

@ -1,3 +1,4 @@
local store = require(script.Parent.Parent.store)
local types = require(script.Parent.Parent.types)
type Atom<T> = types.Atom<T>
type SyncPayload = types.SyncPayload
@ -36,13 +37,17 @@ local function client(options: ClientOptions): ClientSyncer
for index = 1, select("#", ...) do
local payload = select(index, ...)
for key, state in next, payload.data do
if payload.type == "patch" then
atoms[key](patch.apply(atoms[key](), state))
else
atoms[key](state)
store.batch(function()
for key, state in next, payload.data do
local atom = atoms[key]
if payload.type == "patch" then
atom(patch.apply(atom(), state))
else
atom(state)
end
end
end
end)
end
end