tweak: utilise use-layout-effect to safely update atom

This commit is contained in:
Sean Hagstrom 2024-04-01 18:28:50 +01:00 committed by Icaro Motta
parent 5ff9097410
commit 757b8ebcac
No known key found for this signature in database
GPG Key ID: 009557D9D014DF07
1 changed files with 9 additions and 3 deletions

View File

@ -92,12 +92,18 @@
(defn make-snapshot-handler-factory [snapshot]
(let [storage (rn/use-memo #(atom {}) [])
capture (rn/use-memo #(atom snapshot) [])
_update (rn/use-layout-effect
(fn []
(swap! capture (fn [_] snapshot)))
snapshot)
factory (rn/use-memo
(do (swap! capture (fn [_] snapshot))
#(memo storage
(fn []
(fn [handler]
((memo storage
(fn [callback]
(fn [event]
(callback @capture event)))))
(callback @capture event))))
handler)))
[storage capture])]
{:factory factory
:storage storage}))