mirror of
https://github.com/littensy/charm.git
synced 2026-10-23 13:54:07 +00:00
feat: add state sync
This commit is contained in:
parent
5fe86ef030
commit
17a8c48e54
9 changed files with 262 additions and 0 deletions
31
src/sync/client.luau
Normal file
31
src/sync/client.luau
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue