/
busy.cljs
38 lines (32 loc) · 1.25 KB
/
busy.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns dgknght.app-lib.busy
(:require [reagent.core :as r]
[reagent.ratom :refer [make-reaction]]
[dgknght.app-lib.core :refer [fmin]]))
(defn busy
"Given a state atom, create a derefable value that indicates whether or not
the application is running any background processes."
[state]
(let [bg-proc-count (r/cursor state [:bg-proc-count])]
(make-reaction #(when @bg-proc-count
(not (zero? @bg-proc-count))))))
(defn derefable-apply
[target f]
(if (satisfies? IDeref target)
(swap! target f)
(f target)))
(defn +busy
"Given a state map, increment the :bg-proc-count value, indicating
another background process has been started.
The state map can be wrapped in an atom or other derefable type, or
it can be a simple clojure map."
([state] (+busy state :bg-proc-count))
([state k]
(derefable-apply state #(update-in % [k] (fnil inc 0)))))
(defn -busy
"Given a state map, decrement the :bg-proc-count value, indicating
another background process has completed..
The state map can be wrapped in an atom or other derefable type, or
it can be a simple clojure map. "
([state] (-busy state :bg-proc-count))
([state k]
(derefable-apply state #(update-in % [k] (fmin (fnil dec 1) 0)))))