-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.cljs
93 lines (76 loc) · 2.39 KB
/
notifications.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(ns dgknght.app-lib.notifications
(:require [cljs.core.async :as a]
[reagent.core :as r]
[goog.string :as gstr]
[cljs-time.core :as t]))
(defonce notifications (r/atom []))
(defonce toasts (r/atom []))
(def ^:const toast-delay 2000)
(def ^:const toast-sweep-interval 2500)
(defn notify
([message] (notify message :info))
([message severity]
(let [notification {:message message
:severity severity
:id (random-uuid)}]
(swap! notifications #(conj % notification)))))
(defn success [message] (notify message :success))
(defn info [message] (notify message :info))
(defn warning [message] (notify message :warning))
(defn warn [message] (notify message :warning))
(defn danger [message] (notify message :danger))
(defn successf
[message & args]
(notify (apply gstr/format message args) :success))
(defn infof
[message & args]
(notify (apply gstr/format message args) :info))
(defn warningf
[message & args]
(notify (apply gstr/format message args) :warning))
(defn warnf
[message & args]
(notify (apply gstr/format message args) :warning))
(defn dangerf
[message & args]
(notify (apply gstr/format message args) :danger))
(defn danger-fn
[msg-format]
(fn [msg]
(danger (gstr/format msg-format (or (:message msg) msg)))))
(defn unnotify
[notification]
(swap! notifications (fn [notifications]
(remove #(= (:id %) (:id notification))
notifications))))
(declare sweep-toasts)
(defn queue-toast-sweep []
(a/go
(a/<! (a/timeout toast-sweep-interval))
(sweep-toasts)))
(defn toast
([body] (toast nil body))
([title body]
(swap! toasts conj (cond-> {:body body
:expires-at (t/from-now (t/seconds 2))
:id (str (random-uuid))}
title (assoc :title title)))
(queue-toast-sweep)))
(defn toastf
[title body-pattern & args]
(toast title (apply gstr/format body-pattern args)))
(defn untoast
[id]
(swap! toasts (fn [toasts]
(remove #(= id (:id %)) toasts))))
(defn- expired-toast?
[{:keys [expires-at]}]
(t/after? (t/now) expires-at))
(defn sweep-toasts
([]
(swap! toasts sweep-toasts))
([toasts]
(let [result (remove expired-toast? toasts)]
(when (seq result)
(queue-toast-sweep))
result)))