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:
richard 2024-03-25 19:53:28 -07:00 • committed by GitHub
parent 4d4979daa9
commit 33ecae2f50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 577 additions and 729 deletions

View file

@ -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