mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
feat: initial commit
This commit is contained in:
commit
18723c1377
50 changed files with 3238 additions and 0 deletions
19
src/react/useAtom.luau
Normal file
19
src/react/useAtom.luau
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
local atom = require(script.Parent.Parent.atom)
|
||||
type Atom<T> = atom.Atom<T>
|
||||
|
||||
local useAtomState = require(script.Parent.useAtomState)
|
||||
local useSetAtom = require(script.Parent.useSetAtom)
|
||||
|
||||
type UseAtom =
|
||||
(<State>(atom: Atom<State>) -> (State, useSetAtom.SetState<State>))
|
||||
& (<State, Result>(
|
||||
atom: Atom<State>,
|
||||
selector: ((State) -> Result)?
|
||||
) -> (State | Result, useSetAtom.SetState<State | Result>))
|
||||
& (<State, Result>(atom: Atom<State>, selector: (State) -> Result) -> (Result, useSetAtom.SetState<State>))
|
||||
|
||||
local function useAtom<State>(atom: Atom<State>, selector: ((State) -> State)?): (State, useSetAtom.SetState<State>)
|
||||
return useAtomState(atom, selector), useSetAtom(atom)
|
||||
end
|
||||
|
||||
return useAtom :: UseAtom
|
||||
35
src/react/useAtomState.luau
Normal file
35
src/react/useAtomState.luau
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
local React = require(script.Parent.Parent.modules.React)
|
||||
|
||||
local atom = require(script.Parent.Parent.atom)
|
||||
type Atom<T> = atom.Atom<T>
|
||||
|
||||
type UseAtomState =
|
||||
(<State>(atom: Atom<State>) -> State)
|
||||
& (<State, Result>(atom: Atom<State>, ((State) -> Result)?) -> State | Result)
|
||||
& <State, Result>(atom: Atom<State>, selector: (State) -> Result) -> Result
|
||||
|
||||
local function useAtomState<State>(atom: Atom<State>, selector: ((State) -> State)?): State
|
||||
local state, setState = React.useState(function()
|
||||
return atom:get(selector :: (State) -> State)
|
||||
end)
|
||||
|
||||
local latestSelector = React.useRef(selector)
|
||||
latestSelector.current = selector
|
||||
|
||||
React.useEffect(function()
|
||||
local function listener(state: State)
|
||||
local selector = latestSelector.current
|
||||
setState(if selector then selector(state) else state)
|
||||
end
|
||||
|
||||
atom.listeners[listener] = true
|
||||
|
||||
return function()
|
||||
atom.listeners[listener] = nil
|
||||
end
|
||||
end, { atom })
|
||||
|
||||
return state
|
||||
end
|
||||
|
||||
return useAtomState :: UseAtomState
|
||||
14
src/react/useSetAtom.luau
Normal file
14
src/react/useSetAtom.luau
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
local React = require(script.Parent.Parent.modules.React)
|
||||
|
||||
local atom = require(script.Parent.Parent.atom)
|
||||
type Atom<T> = atom.Atom<T>
|
||||
|
||||
export type SetState<T> = (T | ((T) -> T)) -> ()
|
||||
|
||||
local function useSetAtom<State>(atom: Atom<State>): SetState<State>
|
||||
return React.useCallback(function(value)
|
||||
atom:set(value)
|
||||
end, { atom })
|
||||
end
|
||||
|
||||
return useSetAtom
|
||||
Loading…
Add table
Add a link
Reference in a new issue