mirror of
https://github.com/littensy/charm.git
synced 2026-10-23 22:21:37 +00:00
refactor!: rename derive, map
This commit is contained in:
parent
f2f7a16599
commit
b715b81fec
18 changed files with 163 additions and 133 deletions
26
src/computed.luau
Normal file
26
src/computed.luau
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
local atom = require(script.Parent.atom)
|
||||
local store = require(script.Parent.store)
|
||||
local types = require(script.Parent.types)
|
||||
type Atom<T> = types.Atom<T>
|
||||
type AtomOptions<T> = types.AtomOptions<T>
|
||||
type Molecule<T> = types.Molecule<T>
|
||||
|
||||
local function computed<T>(molecule: Molecule<T>, options: AtomOptions<T>?): Molecule<T>
|
||||
local captured, state = store.capture(molecule)
|
||||
local computedAtom = atom(state, options)
|
||||
local computedRef = setmetatable({ current = computedAtom }, { __mode = "v" })
|
||||
|
||||
local function listener()
|
||||
if computedRef.current then
|
||||
computedRef.current(molecule())
|
||||
end
|
||||
end
|
||||
|
||||
for atom in captured do
|
||||
store.listeners[atom][listener] = computedAtom
|
||||
end
|
||||
|
||||
return computedAtom
|
||||
end
|
||||
|
||||
return computed
|
||||
Loading…
Add table
Add a link
Reference in a new issue