Optimize computed's anti-recursion device

This commit is contained in:
boyned//Kampfkarren 2025-03-24 15:53:19 -07:00 • committed by GitHub
parent d7a1f62c3c
commit 3cd6cd54a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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