refactor(sync): destructure options argument

This commit is contained in:
Richard 2025-01-25 14:19:26 -08:00
parent c7c5601e7c
commit d703990e5b
2 changed files with 17 additions and 12 deletions

View file

@ -33,10 +33,10 @@ type ClientSyncer = {
@return A `ClientSyncer` object. @return A `ClientSyncer` object.
]=] ]=]
local function client(options: ClientOptions): ClientSyncer local function client(options: ClientOptions): ClientSyncer
local self = {} :: ClientSyncer
local atoms = options.atoms local atoms = options.atoms
local ignoreUnhydrated = options.ignoreUnhydrated ~= false local ignoreUnhydrated = options.ignoreUnhydrated ~= false
local self = {} :: ClientSyncer
local hydrated = false local hydrated = false
local function hydrate(state: { [string | number]: any }) local function hydrate(state: { [string | number]: any })

View file

@ -67,13 +67,18 @@ type ServerSyncer = {
@return A `ServerSyncer` object. @return A `ServerSyncer` object.
]=] ]=]
local function server(options: ServerOptions): ServerSyncer local function server(options: ServerOptions): ServerSyncer
local atoms = options.atoms
local autoSerialize = options.autoSerialize ~= false
local preserveHistory = options.preserveHistory
local syncRate = options.interval or 0
local self = {} :: ServerSyncer local self = {} :: ServerSyncer
local sync: (player: Player, payload: SyncPayload) -> () local sync: (player: Player, payload: SyncPayload) -> ()
local function createSnapshot() local function createSnapshot()
local snapshot = {} local snapshot = {}
for key, atom in next, options.atoms do for key, atom in next, atoms do
snapshot[key] = atom() snapshot[key] = atom()
end end
@ -92,7 +97,7 @@ local function server(options: ServerOptions): ServerSyncer
-- If the history of state changes should be preserved, keep a list of -- If the history of state changes should be preserved, keep a list of
-- previous snapshots and diff-check each one from the last. -- previous snapshots and diff-check each one from the last.
if options.preserveHistory then if preserveHistory then
local snapshots = { createSnapshot() } local snapshots = { createSnapshot() }
local changed = false local changed = false
@ -119,7 +124,7 @@ local function server(options: ServerOptions): ServerSyncer
-- Populate the snapshot with the initial state of each atom. -- Populate the snapshot with the initial state of each atom.
-- Subscribe to each atom and update the state when it changes. -- Subscribe to each atom and update the state when it changes.
for key, atom in next, options.atoms do for key, atom in next, atoms do
subscriptions[key] = Charm.subscribe(atom, function(current, previous) subscriptions[key] = Charm.subscribe(atom, function(current, previous)
pushSnapshot(key, current, previous) pushSnapshot(key, current, previous)
changed = true changed = true
@ -143,7 +148,7 @@ local function server(options: ServerOptions): ServerSyncer
table.insert(payloads, { table.insert(payloads, {
type = "patch", type = "patch",
data = patch.diff(snapshots[index - 1], snapshot, options.autoSerialize), data = patch.diff(snapshots[index - 1], snapshot, autoSerialize),
}) })
end end
@ -153,7 +158,7 @@ local function server(options: ServerOptions): ServerSyncer
for _, player in next, Players:GetPlayers() do for _, player in next, Players:GetPlayers() do
callback(player, unpack(payloads)) callback(player, unpack(payloads))
end end
end, options.interval or 0) end, syncRate)
return function() return function()
disconnect() disconnect()
@ -180,7 +185,7 @@ local function server(options: ServerOptions): ServerSyncer
table.insert(payloads, { table.insert(payloads, {
type = "patch", type = "patch",
data = patch.diff(snapshots[index - 1], snapshot, options.autoSerialize), data = patch.diff(snapshots[index - 1], snapshot, autoSerialize),
}) })
end end
@ -203,7 +208,7 @@ local function server(options: ServerOptions): ServerSyncer
sync = callback sync = callback
-- Subscribe to each atom and update the state when one changes. -- Subscribe to each atom and update the state when one changes.
for key, atom in next, options.atoms do for key, atom in next, atoms do
subscriptions[key] = Charm.subscribe(atom, function(state) subscriptions[key] = Charm.subscribe(atom, function(state)
currentSnapshot[key] = state currentSnapshot[key] = state
changed = true changed = true
@ -217,7 +222,7 @@ local function server(options: ServerOptions): ServerSyncer
local payload: SyncPayload = { local payload: SyncPayload = {
type = "patch", type = "patch",
data = patch.diff(previousSnapshot, currentSnapshot, options.autoSerialize), data = patch.diff(previousSnapshot, currentSnapshot, autoSerialize),
} }
previousSnapshot = table.clone(currentSnapshot) previousSnapshot = table.clone(currentSnapshot)
@ -226,7 +231,7 @@ local function server(options: ServerOptions): ServerSyncer
for _, player in next, Players:GetPlayers() do for _, player in next, Players:GetPlayers() do
callback(player, payload) callback(player, payload)
end end
end, options.interval or 0) end, syncRate)
return function() return function()
disconnect() disconnect()
@ -246,7 +251,7 @@ local function server(options: ServerOptions): ServerSyncer
local payload: SyncPayload = { local payload: SyncPayload = {
type = "patch", type = "patch",
data = patch.diff(previousSnapshot, currentSnapshot, options.autoSerialize), data = patch.diff(previousSnapshot, currentSnapshot, autoSerialize),
} }
previousSnapshot = table.clone(currentSnapshot) previousSnapshot = table.clone(currentSnapshot)