mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
Optimize computed's anti-recursion device
This commit is contained in:
parent
d7a1f62c3c
commit
3cd6cd54a6
1 changed files with 7 additions and 2 deletions
|
|
@ -18,13 +18,18 @@ local function computed<T>(callback: Selector<T>, options: AtomOptions<T>?): Sel
|
|||
local computedAtom = atom(state, options)
|
||||
local computedRef = setmetatable({ current = computedAtom }, { __mode = "v" })
|
||||
|
||||
local pauseListening = false
|
||||
local function listener()
|
||||
if pauseListening then
|
||||
return
|
||||
end
|
||||
|
||||
local computedAtom = computedRef.current
|
||||
|
||||
if computedAtom then
|
||||
store.disconnect(dependencies, listener)
|
||||
pauseListening = true
|
||||
dependencies, state = store.capture(callback)
|
||||
store.connect(dependencies, listener, computedAtom)
|
||||
pauseListening = false
|
||||
computedAtom(state)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue