mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
refactor!: design overhaul (#1)
* refactor!: use atom functions * docs: reformat api reference * docs: showcase atom functions * docs: remove unused import * refactor: finalize updates * chore: misc edits
This commit is contained in:
parent
4d4979daa9
commit
33ecae2f50
39 changed files with 577 additions and 729 deletions
|
|
@ -1,5 +1,5 @@
|
|||
local atom = require(script.Parent.Parent.atom)
|
||||
type Atom<T> = atom.Atom<T>
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
local React = require(script.Parent.Parent.modules.React)
|
||||
local requireReact = require(script.Parent.Parent.modules.React)
|
||||
local subscribe = require(script.Parent.Parent.subscribe)
|
||||
|
||||
local atom = require(script.Parent.Parent.atom)
|
||||
type Atom<T> = atom.Atom<T>
|
||||
local types = require(script.Parent.Parent.types)
|
||||
type Atom<T> = types.Atom<T>
|
||||
|
||||
type UseAtomState =
|
||||
(<State>(atom: Atom<State>) -> State)
|
||||
|
|
@ -9,24 +10,20 @@ type UseAtomState =
|
|||
& (<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 atom:get(selector :: (State) -> State)
|
||||
return if selector then atom(selector) else atom()
|
||||
end)
|
||||
|
||||
local latestSelector = React.useRef(selector)
|
||||
latestSelector.current = selector
|
||||
|
||||
React.useEffect(function()
|
||||
local function listener(state: State)
|
||||
return subscribe(atom, function(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)
|
||||
end, { atom })
|
||||
|
||||
return state
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
local React = require(script.Parent.Parent.modules.React)
|
||||
local requireReact = require(script.Parent.Parent.modules.React)
|
||||
|
||||
local atom = require(script.Parent.Parent.atom)
|
||||
type Atom<T> = atom.Atom<T>
|
||||
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>
|
||||
return React.useCallback(function(value)
|
||||
atom:set(value)
|
||||
return requireReact().useCallback(function(state: State | ((State) -> State)): ()
|
||||
atom(state)
|
||||
end, { atom })
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue