mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
fix: wrap hooks in untracked, update types
This commit is contained in:
parent
1657e2cb74
commit
87e7260c16
5 changed files with 40 additions and 16 deletions
6
packages/charm-sync/src/index.d.ts
vendored
6
packages/charm-sync/src/index.d.ts
vendored
|
|
@ -115,7 +115,7 @@ export namespace client {
|
|||
|
||||
export function reset(): void;
|
||||
|
||||
export function applyPayloads<Atoms extends { [key: string]: Atom<any> }, Serialize extends boolean>(
|
||||
export function applyPayloads<Atoms extends { [key: string]: Atom<any> }, Serialize extends boolean = true>(
|
||||
payloads: SyncPayload<Atoms, Serialize>[],
|
||||
): void;
|
||||
}
|
||||
|
|
@ -127,8 +127,8 @@ export namespace server {
|
|||
|
||||
export function unsyncClient(client: Player): void;
|
||||
|
||||
export function startSync<Atoms extends { [key: string]: Atom<any> }, Serialize extends boolean>(
|
||||
onSync: (client: Player, payload: SyncPayload<Atoms, Serialize>) => void,
|
||||
export function startSync<Atoms extends { [key: string]: Atom<any> }, Serialize extends boolean = true>(
|
||||
onSync: (client: Player, payloads: SyncPayload<Atoms, Serialize>[]) => void,
|
||||
): Cleanup;
|
||||
|
||||
export function stopSync(): void;
|
||||
|
|
|
|||
|
|
@ -301,7 +301,7 @@ local function startSync(onSync: (client: any, payloads: { SyncPayload }) -> ())
|
|||
timer += deltaTime
|
||||
|
||||
if timer >= config.interval then
|
||||
timer %= config.interval
|
||||
timer = 0
|
||||
debug.profilebegin("CharmSync")
|
||||
flush()
|
||||
debug.profileend()
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ local function useAtom<T>(getter: () -> T, dependencies: { any }?): T
|
|||
|
||||
React.useLayoutEffect(function()
|
||||
return Charm.effect(function()
|
||||
setState(getter())
|
||||
Charm.untracked(setState, getter())
|
||||
end)
|
||||
end, dependencies or {})
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ local function useAtomBinding<T>(getter: () -> T, dependencies: { any }?)
|
|||
|
||||
React.useLayoutEffect(function()
|
||||
return Charm.effect(function()
|
||||
setBinding(getter())
|
||||
Charm.untracked(setBinding, getter())
|
||||
end)
|
||||
end, dependencies or {})
|
||||
|
||||
|
|
|
|||
|
|
@ -3,16 +3,13 @@ local Vide = require("../../Vide")
|
|||
local VideCharm = require("../")
|
||||
local suite = require("../../../../test/suite")
|
||||
|
||||
local atom = Charm.atom
|
||||
local root = Vide.root
|
||||
local useAtom = VideCharm.useAtom
|
||||
local test = suite.test
|
||||
|
||||
test("tracks atom state", function()
|
||||
local state = atom(0)
|
||||
local state = Charm.atom(0)
|
||||
local source
|
||||
local cleanup = root(function()
|
||||
source = useAtom(state)
|
||||
local cleanup = Vide.root(function()
|
||||
source = VideCharm.useAtom(state)
|
||||
end)
|
||||
assert(source() == 0, "should return 0")
|
||||
state(1)
|
||||
|
|
@ -21,10 +18,10 @@ test("tracks atom state", function()
|
|||
end)
|
||||
|
||||
test("accepts a selector", function()
|
||||
local state = atom(0)
|
||||
local state = Charm.atom(0)
|
||||
local source
|
||||
local cleanup = root(function()
|
||||
source = useAtom(function()
|
||||
local cleanup = Vide.root(function()
|
||||
source = VideCharm.useAtom(function()
|
||||
return 2 * state()
|
||||
end)
|
||||
end)
|
||||
|
|
@ -34,4 +31,31 @@ test("accepts a selector", function()
|
|||
cleanup()
|
||||
end)
|
||||
|
||||
test("updates source in untracked", function()
|
||||
local outerAtom = Charm.atom(nil :: any)
|
||||
local innerAtom = Charm.atom(0)
|
||||
local spyCalls = 0
|
||||
local cleanup = Vide.root(function()
|
||||
return Vide.show(VideCharm.useAtom(outerAtom), function()
|
||||
-- Charm effects created in Vide scopes triggered by useAtom might
|
||||
-- incorrectly get treated as an inner effect in useAtom. This test
|
||||
-- ensures that doesn't happen.
|
||||
Vide.cleanup(Charm.effect(function()
|
||||
innerAtom()
|
||||
spyCalls += 1
|
||||
end))
|
||||
return nil
|
||||
end)
|
||||
end)
|
||||
assert(spyCalls == 0, "should not call spy initially")
|
||||
outerAtom(true)
|
||||
assert(spyCalls == 1, "should call spy when visible")
|
||||
innerAtom(1)
|
||||
assert(spyCalls == 2, "spy should run when counter updates")
|
||||
outerAtom({}) -- set to a truthy value to trigger an outer effect re-run
|
||||
innerAtom(2)
|
||||
assert(spyCalls == 3, "spy should still run when counter updates")
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ local function useAtom<State>(getter: () -> State): () -> State
|
|||
local state = Vide.source(getter())
|
||||
|
||||
Vide.cleanup(Charm.effect(function()
|
||||
state(getter())
|
||||
Charm.untracked(state :: (State) -> State, getter())
|
||||
end))
|
||||
|
||||
return state
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue