mirror of
https://github.com/littensy/charm.git
synced 2026-10-23 05:46:27 +00:00
fix: avoid reconnecting to dependencies after cleanup (#14)
* fix: cancel reconnection when unsubscribing * test: remove print * feat: allow factory to return nil * perf: use `next` iteration
This commit is contained in:
parent
62be3e496d
commit
c6762a6525
14 changed files with 87 additions and 63 deletions
|
|
@ -36,7 +36,7 @@ local function client(options: ClientOptions): ClientSyncer
|
|||
for index = 1, select("#", ...) do
|
||||
local payload = select(index, ...)
|
||||
|
||||
for key, state in payload.data do
|
||||
for key, state in next, payload.data do
|
||||
if payload.type == "patch" then
|
||||
atoms[key](patch.apply(atoms[key](), state))
|
||||
else
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ end
|
|||
local function diff(prevState: { [any]: any }, nextState: { [any]: any })
|
||||
local patches = table.clone(nextState)
|
||||
|
||||
for key, previous in prevState do
|
||||
for key, previous in next, prevState do
|
||||
local next = nextState[key]
|
||||
|
||||
if previous == next then
|
||||
|
|
@ -26,7 +26,7 @@ local function diff(prevState: { [any]: any }, nextState: { [any]: any })
|
|||
end
|
||||
|
||||
if _G.__DEV__ then
|
||||
for key, value in prevState do
|
||||
for key, value in next, prevState do
|
||||
validate(value, key)
|
||||
end
|
||||
|
||||
|
|
@ -50,7 +50,7 @@ local function apply(state: any, patches: any): any
|
|||
local nextState = table.clone(state)
|
||||
local stateIsArray = state[1] ~= nil
|
||||
|
||||
for key, patch in patches do
|
||||
for key, patch in next, patches do
|
||||
-- Diff-checking an array produces a sparse array, which will not be
|
||||
-- preserved when converted to JSON. To prevent this, we turn string
|
||||
-- keys back into numeric keys.
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
|
||||
local function getSnapshot()
|
||||
local snapshot: { [string]: any } = {}
|
||||
for key, atom in options.atoms do
|
||||
for key, atom in next, options.atoms do
|
||||
snapshot[key] = atom()
|
||||
end
|
||||
return snapshot
|
||||
|
|
@ -105,7 +105,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
|
||||
-- Populate the initial state and snapshot for each atom.
|
||||
-- Subscribe to each atom and update the state when it changes.
|
||||
for key, atom in options.atoms do
|
||||
for key, atom in next, options.atoms do
|
||||
cleanups[key] = subscribe(atom, function(current, previous)
|
||||
pushSnapshot(key, current, previous)
|
||||
changed = true
|
||||
|
|
@ -122,7 +122,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
local payloads: { SyncPayload } = {}
|
||||
local lastSnapshot
|
||||
|
||||
for index, snapshot in snapshots do
|
||||
for index, snapshot in next, snapshots do
|
||||
lastSnapshot = snapshot
|
||||
|
||||
if index == 1 then
|
||||
|
|
@ -138,14 +138,14 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
snapshots = { lastSnapshot }
|
||||
changed = false
|
||||
|
||||
for _, player in Players:GetPlayers() do
|
||||
for _, player in next, Players:GetPlayers() do
|
||||
callback(player, unpack(payloads))
|
||||
end
|
||||
end, options.interval or 0)
|
||||
|
||||
return function()
|
||||
disconnect()
|
||||
for _, cleanup in cleanups do
|
||||
for _, cleanup in next, cleanups do
|
||||
cleanup()
|
||||
end
|
||||
end
|
||||
|
|
@ -170,7 +170,7 @@ local function server(options: ServerOptions): ServerSyncer
|
|||
|
||||
local payloads: { SyncPayload } = {}
|
||||
|
||||
for index, snapshot in snapshots do
|
||||
for index, snapshot in next, snapshots do
|
||||
if index == 1 then
|
||||
continue
|
||||
end
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ local function isUnsafeTable(object: { [any]: any })
|
|||
local objectSize = 0
|
||||
|
||||
-- All keys must have the same type
|
||||
for key in object do
|
||||
for key in next, object do
|
||||
local currentType = type(key)
|
||||
|
||||
if not keyType and SAFE_KEYS[currentType] then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue