feat: add state sync

This commit is contained in:
Richard 2024-02-27 22:20:18 -08:00
parent 5fe86ef030
commit 17a8c48e54
9 changed files with 262 additions and 0 deletions

31
src/sync/client.luau Normal file
View file

@ -0,0 +1,31 @@
local atom = require(script.Parent.Parent.atom)
type Atom<T> = atom.Atom<T>
local patch = require(script.Parent.patch)
local types = require(script.Parent.types)
type ClientSyncOptions = {
atoms: { [string]: Atom<any> },
}
type ClientSync = {
sync: (self: ClientSync, payload: types.SyncPayload) -> (),
}
local function client(options: ClientSyncOptions): ClientSync
local atoms = options.atoms
local self = {} :: ClientSync
function self:sync(payload)
for key, state in payload.data do
if payload.type == "patch" then
atoms[key]:set(patch.apply(atoms[key]:get(), state))
else
atoms[key]:set(state)
end
end
end
return self
end
return client