mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
refactor!: minimal react api
This commit is contained in:
parent
047be71eaa
commit
f2f7a16599
10 changed files with 59 additions and 144 deletions
|
|
@ -1,18 +1,19 @@
|
|||
local requireReact = require(script.Parent.Parent.modules.React)
|
||||
local subscribe = require(script.Parent.Parent.subscribe)
|
||||
local types = require(script.Parent.Parent.types)
|
||||
type Atom<T> = types.Atom<T>
|
||||
local useAtomState = require(script.Parent.useAtomState)
|
||||
local useSetAtom = require(script.Parent.useSetAtom)
|
||||
type Molecule<T> = types.Molecule<T>
|
||||
|
||||
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>(molecule: Molecule<State>, dependencies: { any }?): State
|
||||
local React = requireReact()
|
||||
|
||||
local function useAtom<State>(atom: Atom<State>, selector: ((State) -> State)?): (State, useSetAtom.SetState<State>)
|
||||
return useAtomState(atom, selector), useSetAtom(atom)
|
||||
local state, setState = React.useState(molecule)
|
||||
|
||||
React.useEffect(function()
|
||||
setState(molecule())
|
||||
return subscribe(molecule, setState)
|
||||
end, dependencies or {})
|
||||
|
||||
return state
|
||||
end
|
||||
|
||||
return useAtom :: UseAtom
|
||||
return useAtom
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
local requireReact = require(script.Parent.Parent.modules.React)
|
||||
local subscribe = require(script.Parent.Parent.subscribe)
|
||||
local types = require(script.Parent.Parent.types)
|
||||
type Atom<T> = types.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 React = requireReact()
|
||||
|
||||
local state, setState = React.useState(function()
|
||||
return if selector then selector(atom()) else atom()
|
||||
end)
|
||||
|
||||
local latestSelector = React.useRef(selector)
|
||||
latestSelector.current = selector
|
||||
|
||||
React.useEffect(function()
|
||||
return subscribe(atom, function(state)
|
||||
local selector = latestSelector.current
|
||||
setState(if selector then selector(state) else state)
|
||||
end)
|
||||
end, { atom })
|
||||
|
||||
return state
|
||||
end
|
||||
|
||||
return useAtomState :: UseAtomState
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
local requireReact = require(script.Parent.Parent.modules.React)
|
||||
local types = require(script.Parent.Parent.types)
|
||||
type Atom<T> = types.Atom<T>
|
||||
|
||||
export type SetState<T> = (T | ((T) -> T)) -> ()
|
||||
|
||||
local function useSetAtom<State>(atom: Atom<State>): SetState<State>
|
||||
local React = requireReact()
|
||||
|
||||
return React.useCallback(function(state: State | ((State) -> State)): ()
|
||||
atom(state)
|
||||
end, { atom })
|
||||
end
|
||||
|
||||
return useSetAtom
|
||||
Loading…
Add table
Add a link
Reference in a new issue