mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 11:44:08 +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,31 +1,30 @@
|
|||
local Charm = require("./Charm")
|
||||
local React = require("./React")
|
||||
|
||||
--- A hook that subscribes to changes in the given atom or selector. The
|
||||
--- component is re-rendered with the new state after an update.
|
||||
--- Returns a React state that updates when the getter function updates.
|
||||
--- Re-renders the component when the value changes.
|
||||
---
|
||||
--- If the `dependencies` array is provided, a new effect is created for the
|
||||
--- current `getter` when the dependencies change. Otherwise, the effect is
|
||||
--- created only once, when the component is mounted.
|
||||
---
|
||||
--- @param getter The atom or selector to watch.
|
||||
--- @param getter The signal or selector to watch.
|
||||
--- @param dependencies An array of values used to memoize the getter.
|
||||
--- @return The current state.
|
||||
local function useAtom<T>(getter: () -> T, dependencies: { any }?): T
|
||||
local state, setState = React.useState(getter)
|
||||
local function useSignalState<T>(getter: () -> T, dependencies: { any }?): T
|
||||
local value, setValue = React.useState(getter)
|
||||
|
||||
React.useLayoutEffect(function()
|
||||
return Charm.effect(function()
|
||||
-- Wrap in untracked to avoid tracking inner effect and atom calls
|
||||
Charm.untracked(setState, getter())
|
||||
Charm.untracked(setValue, getter())
|
||||
end)
|
||||
end, dependencies or {})
|
||||
|
||||
return state
|
||||
return value
|
||||
end
|
||||
|
||||
--- A hook that subscribes to changes in the given atom or selector. Returns
|
||||
--- a binding that updates when the state changes.
|
||||
--- Returns a React binding that updates when the getter function updates.
|
||||
---
|
||||
--- If the `dependencies` array is provided, a new effect is created for the
|
||||
--- current `getter` when the dependencies change. Otherwise, the effect is
|
||||
|
|
@ -34,9 +33,9 @@ end
|
|||
--- @param getter The atom or selector to watch.
|
||||
--- @param dependencies An array of values used to memoize the getter.
|
||||
--- @return A binding that stores the current state.
|
||||
local function useAtomBinding<T>(getter: () -> T, dependencies: { any }?)
|
||||
local initialState = React.useMemo(getter, {})
|
||||
local binding, setBinding = React.useBinding(initialState)
|
||||
local function useSignalBinding<T>(getter: () -> T, dependencies: { any }?)
|
||||
local initialValue = React.useMemo(getter, {})
|
||||
local binding, setBinding = React.useBinding(initialValue)
|
||||
|
||||
React.useLayoutEffect(function()
|
||||
return Charm.effect(function()
|
||||
|
|
@ -49,6 +48,7 @@ local function useAtomBinding<T>(getter: () -> T, dependencies: { any }?)
|
|||
end
|
||||
|
||||
return {
|
||||
useAtom = useAtom,
|
||||
useAtomBinding = useAtomBinding,
|
||||
useSignalState = useSignalState,
|
||||
useSignalBinding = useSignalBinding,
|
||||
useAtom = useSignalState,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue