mirror of
https://github.com/littensy/charm.git
synced 2026-10-24 06:51:37 +00:00
feat: allow conditional dependencies (#4)
* feat: allow conditional dependencies * docs: remove conditional deps warnings * fix: allow nested captures * docs: add dev flag * fix: `try()` uses pcall if passed `finally()` * docs: interval is optional * docs: update readme * docs: add client/server tags
This commit is contained in:
parent
fbc86d22c6
commit
a90d131fed
14 changed files with 290 additions and 98 deletions
|
|
@ -15,19 +15,18 @@ type Molecule<T> = types.Molecule<T>
|
|||
@return A new read-only atom.
|
||||
]=]
|
||||
local function computed<T>(molecule: Molecule<T>, options: AtomOptions<T>?): Molecule<T>
|
||||
local captured, state = store.capture(molecule)
|
||||
local dependencies, 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())
|
||||
dependencies, state = store.reconnect(molecule, dependencies, listener, computedRef.current)
|
||||
computedRef.current(state)
|
||||
end
|
||||
end
|
||||
|
||||
for atom in captured do
|
||||
store.listeners[atom][listener] = computedAtom
|
||||
end
|
||||
store.connect(dependencies, listener, computedAtom)
|
||||
|
||||
return computedAtom
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue