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
|
|
@ -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