mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 21:01:38 +00:00
perf: use next iteration
This commit is contained in:
parent
ce5ae96c2b
commit
2e52d1bdae
7 changed files with 15 additions and 15 deletions
|
|
@ -18,7 +18,7 @@ return function()
|
||||||
nested_atom = { value = atom(0), expected = false },
|
nested_atom = { value = atom(0), expected = false },
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, case in cases do
|
for key, case in next, cases do
|
||||||
local success, result = pcall(function()
|
local success, result = pcall(function()
|
||||||
validate(case.value, key)
|
validate(case.value, key)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ local function atom<T>(state: T, options: AtomOptions<T>?): Atom<T>
|
||||||
|
|
||||||
local function atom(...)
|
local function atom(...)
|
||||||
if select("#", ...) == 0 then
|
if select("#", ...) == 0 then
|
||||||
for set in store.capturing do
|
for set in next, store.capturing do
|
||||||
set[atom] = true
|
set[atom] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ local function mapped<K0, V0, K1, V1>(molecule: Molecule<{ [K0]: V0 }>, mapper:
|
||||||
local mappedKeys = {}
|
local mappedKeys = {}
|
||||||
|
|
||||||
-- TODO: Only call mapper if the item has changed.
|
-- TODO: Only call mapper if the item has changed.
|
||||||
for key, item in items do
|
for key, item in next, items do
|
||||||
local newItem, newKey = mapper(item, key)
|
local newItem, newKey = mapper(item, key)
|
||||||
if newKey == nil then
|
if newKey == nil then
|
||||||
newKey = key :: any
|
newKey = key :: any
|
||||||
|
|
@ -48,7 +48,7 @@ local function mapped<K0, V0, K1, V1>(molecule: Molecule<{ [K0]: V0 }>, mapper:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for key in prevMappedItems do
|
for key in next, prevMappedItems do
|
||||||
if mappedKeys[key] == nil and mappedItems[key] == prevMappedItems[key] then
|
if mappedKeys[key] == nil and mappedItems[key] == prevMappedItems[key] then
|
||||||
mappedItems[key] = nil
|
mappedItems[key] = nil
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ local function client(options: ClientOptions): ClientSyncer
|
||||||
for index = 1, select("#", ...) do
|
for index = 1, select("#", ...) do
|
||||||
local payload = select(index, ...)
|
local payload = select(index, ...)
|
||||||
|
|
||||||
for key, state in payload.data do
|
for key, state in next, payload.data do
|
||||||
if payload.type == "patch" then
|
if payload.type == "patch" then
|
||||||
atoms[key](patch.apply(atoms[key](), state))
|
atoms[key](patch.apply(atoms[key](), state))
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ end
|
||||||
local function diff(prevState: { [any]: any }, nextState: { [any]: any })
|
local function diff(prevState: { [any]: any }, nextState: { [any]: any })
|
||||||
local patches = table.clone(nextState)
|
local patches = table.clone(nextState)
|
||||||
|
|
||||||
for key, previous in prevState do
|
for key, previous in next, prevState do
|
||||||
local next = nextState[key]
|
local next = nextState[key]
|
||||||
|
|
||||||
if previous == next then
|
if previous == next then
|
||||||
|
|
@ -26,7 +26,7 @@ local function diff(prevState: { [any]: any }, nextState: { [any]: any })
|
||||||
end
|
end
|
||||||
|
|
||||||
if _G.__DEV__ then
|
if _G.__DEV__ then
|
||||||
for key, value in prevState do
|
for key, value in next, prevState do
|
||||||
validate(value, key)
|
validate(value, key)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -50,7 +50,7 @@ local function apply(state: any, patches: any): any
|
||||||
local nextState = table.clone(state)
|
local nextState = table.clone(state)
|
||||||
local stateIsArray = state[1] ~= nil
|
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
|
-- Diff-checking an array produces a sparse array, which will not be
|
||||||
-- preserved when converted to JSON. To prevent this, we turn string
|
-- preserved when converted to JSON. To prevent this, we turn string
|
||||||
-- keys back into numeric keys.
|
-- keys back into numeric keys.
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ local function server(options: ServerOptions): ServerSyncer
|
||||||
|
|
||||||
local function getSnapshot()
|
local function getSnapshot()
|
||||||
local snapshot: { [string]: any } = {}
|
local snapshot: { [string]: any } = {}
|
||||||
for key, atom in options.atoms do
|
for key, atom in next, options.atoms do
|
||||||
snapshot[key] = atom()
|
snapshot[key] = atom()
|
||||||
end
|
end
|
||||||
return snapshot
|
return snapshot
|
||||||
|
|
@ -105,7 +105,7 @@ local function server(options: ServerOptions): ServerSyncer
|
||||||
|
|
||||||
-- Populate the initial state and snapshot for each atom.
|
-- Populate the initial state and snapshot for 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 options.atoms do
|
for key, atom in next, options.atoms do
|
||||||
cleanups[key] = subscribe(atom, function(current, previous)
|
cleanups[key] = subscribe(atom, function(current, previous)
|
||||||
pushSnapshot(key, current, previous)
|
pushSnapshot(key, current, previous)
|
||||||
changed = true
|
changed = true
|
||||||
|
|
@ -122,7 +122,7 @@ local function server(options: ServerOptions): ServerSyncer
|
||||||
local payloads: { SyncPayload } = {}
|
local payloads: { SyncPayload } = {}
|
||||||
local lastSnapshot
|
local lastSnapshot
|
||||||
|
|
||||||
for index, snapshot in snapshots do
|
for index, snapshot in next, snapshots do
|
||||||
lastSnapshot = snapshot
|
lastSnapshot = snapshot
|
||||||
|
|
||||||
if index == 1 then
|
if index == 1 then
|
||||||
|
|
@ -138,14 +138,14 @@ local function server(options: ServerOptions): ServerSyncer
|
||||||
snapshots = { lastSnapshot }
|
snapshots = { lastSnapshot }
|
||||||
changed = false
|
changed = false
|
||||||
|
|
||||||
for _, player in 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, options.interval or 0)
|
||||||
|
|
||||||
return function()
|
return function()
|
||||||
disconnect()
|
disconnect()
|
||||||
for _, cleanup in cleanups do
|
for _, cleanup in next, cleanups do
|
||||||
cleanup()
|
cleanup()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -170,7 +170,7 @@ local function server(options: ServerOptions): ServerSyncer
|
||||||
|
|
||||||
local payloads: { SyncPayload } = {}
|
local payloads: { SyncPayload } = {}
|
||||||
|
|
||||||
for index, snapshot in snapshots do
|
for index, snapshot in next, snapshots do
|
||||||
if index == 1 then
|
if index == 1 then
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ local function isUnsafeTable(object: { [any]: any })
|
||||||
local objectSize = 0
|
local objectSize = 0
|
||||||
|
|
||||||
-- All keys must have the same type
|
-- All keys must have the same type
|
||||||
for key in object do
|
for key in next, object do
|
||||||
local currentType = type(key)
|
local currentType = type(key)
|
||||||
|
|
||||||
if not keyType and SAFE_KEYS[currentType] then
|
if not keyType and SAFE_KEYS[currentType] then
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue