mirror of
https://github.com/status-im/re-frame.git
synced 2025-02-22 14:58:12 +00:00
Add jvm interop handlers for dispose! and add-on-dispose!
This commit is contained in:
parent
e8301a864d
commit
b7be7864a4
@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
(defonce ^:private executor (Executors/newSingleThreadExecutor))
|
(defonce ^:private executor (Executors/newSingleThreadExecutor))
|
||||||
|
|
||||||
|
(defonce ^:private on-dispose-callbacks (atom {}))
|
||||||
|
|
||||||
(defn next-tick [f]
|
(defn next-tick [f]
|
||||||
(let [bound-f (bound-fn [& args] (apply f args))]
|
(let [bound-f (bound-fn [& args] (apply f args))]
|
||||||
(.execute ^Executor executor bound-f))
|
(.execute ^Executor executor bound-f))
|
||||||
@ -56,15 +58,21 @@
|
|||||||
(deref [_] (f))))
|
(deref [_] (f))))
|
||||||
|
|
||||||
(defn add-on-dispose!
|
(defn add-on-dispose!
|
||||||
"No-op in JVM Clojure, since for testing purposes, we don't care about
|
"On JVM Clojure, use an atom to register `f` to be invoked when `dispose!` is
|
||||||
releasing resources for efficiency purposes."
|
invoked with `a-ratom`."
|
||||||
[a-ratom f]
|
[a-ratom f]
|
||||||
nil)
|
(do (swap! on-dispose-callbacks update a-ratom (fnil conj []) f)
|
||||||
|
nil))
|
||||||
|
|
||||||
(defn dispose! [a-ratom]
|
(defn dispose!
|
||||||
"No-op in JVM Clojure, since for testing purposes, we don't care about
|
"On JVM Clojure, invoke all callbacks registered with `add-on-dispose!` for
|
||||||
releasing resources for efficiency purposes."
|
`a-ratom`."
|
||||||
nil)
|
[a-ratom]
|
||||||
|
;; Try to replicate reagent's behavior, releasing resources first then
|
||||||
|
;; invoking callbacks
|
||||||
|
(let [callbacks (get @on-dispose-callbacks a-ratom)]
|
||||||
|
(swap! on-dispose-callbacks dissoc a-ratom)
|
||||||
|
(doseq [f callbacks] (f))))
|
||||||
|
|
||||||
(defn set-timeout!
|
(defn set-timeout!
|
||||||
"Note that we ignore the `ms` value and just invoke the function, because
|
"Note that we ignore the `ms` value and just invoke the function, because
|
||||||
|
@ -98,6 +98,22 @@
|
|||||||
(subs/subscribe [:side-effecting-handler :a]) ;; this should be handled by cache
|
(subs/subscribe [:side-effecting-handler :a]) ;; this should be handled by cache
|
||||||
(is (= @side-effect-atom 2))))
|
(is (= @side-effect-atom 2))))
|
||||||
|
|
||||||
|
;============== test clear-subscription-cache! ================
|
||||||
|
|
||||||
|
(deftest test-clear-subscription-cache!
|
||||||
|
(re-frame/reg-sub
|
||||||
|
:clear-subscription-cache!
|
||||||
|
(fn clear-subs-cache [db _] 1))
|
||||||
|
|
||||||
|
(testing "cold cache"
|
||||||
|
(is (nil? (subs/cache-lookup [:clear-subscription-cache!]))))
|
||||||
|
(testing "cache miss"
|
||||||
|
(is (= 1 @(subs/subscribe [:clear-subscription-cache!])))
|
||||||
|
(is (some? (subs/cache-lookup [:clear-subscription-cache!]))))
|
||||||
|
(testing "clearing"
|
||||||
|
(subs/clear-subscription-cache!)
|
||||||
|
(is (nil? (subs/cache-lookup [:clear-subscription-cache!])))))
|
||||||
|
|
||||||
;============== test register-pure macros ================
|
;============== test register-pure macros ================
|
||||||
|
|
||||||
(deftest test-reg-sub-macro
|
(deftest test-reg-sub-macro
|
||||||
|
Loading…
x
Reference in New Issue
Block a user