charm/src/sync/client.luau
richard 33ecae2f50
refactor!: design overhaul (#1)
* refactor!: use atom functions

* docs: reformat api reference

* docs: showcase atom functions

* docs: remove unused import

* refactor: finalize updates

* chore: misc edits
2024-03-25 19:53:28 -07:00

31 lines
654 B
Lua

local types = require(script.Parent.Parent.types)
type Atom<T> = types.Atom<T>
type SyncPayload = types.SyncPayload
local patch = require(script.Parent.patch)
type ClientOptions = {
atoms: { [string]: Atom<any> },
}
type ClientSyncer = {
sync: (self: ClientSyncer, payload: SyncPayload) -> (),
}
local function client(options: ClientOptions): ClientSyncer
local atoms = options.atoms
local self = {} :: ClientSyncer
function self:sync(payload)
for key, state in payload.data do
if payload.type == "patch" then
atoms[key](patch.apply(atoms[key](), state))
else
atoms[key](state)
end
end
end
return self
end
return client