mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
refactor: remove unnecessary features, update docs
This commit is contained in:
parent
7a88c784fa
commit
37f79ecc3f
20 changed files with 774 additions and 955 deletions
|
|
@ -1,7 +1,7 @@
|
|||
local suite = require("../../../../test/suite")
|
||||
|
||||
suite.describe("useAtom", function()
|
||||
require("./useAtom.spec")
|
||||
suite.describe("useSignalState", function()
|
||||
require("./useSignalState.spec")
|
||||
end)
|
||||
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -5,55 +5,55 @@ local suite = require("../../../../test/suite")
|
|||
|
||||
local test = suite.test
|
||||
|
||||
test("tracks atom state", function()
|
||||
local state = Charm.atom(0)
|
||||
test("tracks signal state", function()
|
||||
local getState, setState = Charm.signal(0)
|
||||
local source
|
||||
local cleanup = Vide.root(function()
|
||||
source = VideCharm.useAtom(state)
|
||||
source = VideCharm.useSignalState(getState)
|
||||
end)
|
||||
assert(source() == 0, "should return 0")
|
||||
state(1)
|
||||
setState(1)
|
||||
assert(source() == 1, "should return 1")
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
test("accepts a selector", function()
|
||||
local state = Charm.atom(0)
|
||||
local getState, setState = Charm.signal(0)
|
||||
local source
|
||||
local cleanup = Vide.root(function()
|
||||
source = VideCharm.useAtom(function()
|
||||
return 2 * state()
|
||||
source = VideCharm.useSignalState(function()
|
||||
return 2 * getState()
|
||||
end)
|
||||
end)
|
||||
assert(source() == 0, "should return 0")
|
||||
state(1)
|
||||
setState(1)
|
||||
assert(source() == 2, "should return 2")
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
test("updates source in untracked", function()
|
||||
local outerAtom = Charm.atom(nil :: any)
|
||||
local innerAtom = Charm.atom(0)
|
||||
local getOuter, setOuter = Charm.signal(nil :: any)
|
||||
local getInner, setInner = Charm.signal(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
|
||||
return Vide.show(VideCharm.useSignalState(getOuter), function()
|
||||
-- Charm effects created in Vide scopes triggered by useSignalState might
|
||||
-- incorrectly get treated as an inner effect in useSignalState. This test
|
||||
-- ensures that doesn't happen.
|
||||
Vide.cleanup(Charm.effect(function()
|
||||
innerAtom()
|
||||
getInner()
|
||||
spyCalls += 1
|
||||
end))
|
||||
return nil
|
||||
end)
|
||||
end)
|
||||
assert(spyCalls == 0, "should not call spy initially")
|
||||
outerAtom(true)
|
||||
setOuter(true)
|
||||
assert(spyCalls == 1, "should call spy when visible")
|
||||
innerAtom(1)
|
||||
setInner(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)
|
||||
setOuter({}) -- set to a truthy value to trigger an outer effect re-run
|
||||
setInner(2)
|
||||
assert(spyCalls == 3, "spy should still run when counter updates")
|
||||
cleanup()
|
||||
end)
|
||||
13
packages/vide-charm/src/index.d.ts
vendored
13
packages/vide-charm/src/index.d.ts
vendored
|
|
@ -1,8 +1,11 @@
|
|||
/**
|
||||
* Subscribes to the state of an atom or selector. Returns a source that
|
||||
* updates when the state changes.
|
||||
* Returns a reactive source that subscribes to the current value of the given
|
||||
* signal or getter function.
|
||||
*
|
||||
* @param callback The atom or selector to subscribe to.
|
||||
* @returns The reactive source.
|
||||
* @param getter The getter function to subscribe to.
|
||||
* @return The reactive source.
|
||||
*/
|
||||
export function useAtom<T>(callback: () => T, dependencies?: any[]): () => T;
|
||||
export function useSignalState<T>(getter: () => T, dependencies?: any[]): () => T;
|
||||
|
||||
// Aliases
|
||||
export { useSignalState as useAtom };
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
local Charm = require("./Charm")
|
||||
local Vide = require("./Vide")
|
||||
|
||||
--- Returns a reactive source that updates when the given getter function's
|
||||
--- value changes.
|
||||
--- Returns a reactive source that subscribes to the current value of the given
|
||||
--- signal or getter function.
|
||||
---
|
||||
--- @param getter The atom or selector to subscribe to.
|
||||
--- @param getter The getter function to subscribe to.
|
||||
--- @return The reactive source.
|
||||
local function useAtom<State>(getter: () -> State): () -> State
|
||||
local function useSignalState<State>(getter: () -> State): () -> State
|
||||
local state = Vide.source(getter())
|
||||
|
||||
Vide.cleanup(Charm.effect(function()
|
||||
|
|
@ -18,5 +18,6 @@ local function useAtom<State>(getter: () -> State): () -> State
|
|||
end
|
||||
|
||||
return {
|
||||
useAtom = useAtom,
|
||||
useSignalState = useSignalState,
|
||||
useAtom = useSignalState,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue