-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyour_project.cljs
143 lines (113 loc) · 3.86 KB
/
your_project.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(ns ^:figwheel-hooks jtk-dvlp.your-project
(:require
[cljs.pprint]
[ajax.core :as ajax]
[day8.re-frame.http-fx]
[cljs.core.async :refer [timeout]]
[jtk-dvlp.async :refer [go <!]]
[goog.dom :as gdom]
[reagent.dom :as rdom]
[re-frame.core :as rf]
[jtk-dvlp.re-frame.async-coeffects :as rf-acofxs]))
(rf/reg-cofx ::now
(fn [coeffects]
(println "cofx now")
(assoc coeffects ::now (js/Date.))))
(rf-acofxs/reg-acofx ::async-now
(fn [coeffects delay-in-ms]
(go
(let [delay-in-ms
(or delay-in-ms 1000)
start
(js/Date.)]
(println "acofx async-now" delay-in-ms)
(when (> delay-in-ms 10000)
(throw (ex-info "too long delay!" {:code :too-long-delay})))
(<! (timeout delay-in-ms))
(println "acofx async-now finished" delay-in-ms)
(assoc coeffects ::async-now (- (.getTime (js/Date.))(.getTime start)),)))))
(rf-acofxs/reg-acofx-by-fx ::github-repo-meta
:http-xhrio
:on-success
:on-failure
{:method :get
:uri "https://api.github.com/repos/jtkDvlp/re-frame-async-coeffects"
:response-format (ajax/json-response-format {:keywords? true})})
(rf-acofxs/reg-acofx-by-fx ::http-request
:http-xhrio
:on-success
:on-failure
{:method :get
:response-format (ajax/json-response-format {:keywords? true})})
(rf-acofxs/set-global-error-dispatch! [::change-message "ahhhhhh!"])
(rf/reg-event-fx ::do-work-with-async-stuff
[(rf-acofxs/inject-acofx ::async-now) ; Inject one single acofx without error-dispatch (global set error-dispatch will be used)
(rf-acofxs/inject-acofxs ; Inject multiple acofxs and renames keys within coeffects map.
{::async-now*
::async-now
::async-now-5-secs-delayed
[::async-now 5000] ; Inject with one value arg
::async-now-x-secs-delayed
[::async-now #(get-in % [:db ::delay] 0)] ; Inject with one fn arg
::github-repo-meta
::github-repo-meta
::re-frame-tasks-meta
[::http-request {:uri "https://api.github.com/repos/jtkDvlp/re-frame-tasks"}]
::core.async-helpers-meta
[::http-request {:uri "https://api.github.com/repos/jtkDvlp/core.async-helpers"}]}
{:error-dispatch [::change-message "ahhhhhh!"]} ; Overrides global set error-dispatch for these acofxs
,,,)
(rf/inject-cofx ::now) ; Inject normal cofx
]
(fn [{:keys [db] :as cofxs} _]
(let [async-computed-results
(-> cofxs
(update ::github-repo-meta (comp :description))
(update ::re-frame-tasks-meta (comp :description))
(update ::core.async-helpers-meta (comp :description))
(dissoc :db :event :original-event))]
{:db
(-> db
(assoc ::async-computed-results async-computed-results)
(assoc ::message nil))})))
(rf/reg-sub ::async-computed-results
(fn [db]
(::async-computed-results db)))
(rf/reg-event-db ::change-delay
(fn [db [_ delay]]
(assoc db ::delay delay)))
(rf/reg-sub ::delay
(fn [db]
(::delay db 0)))
(rf/reg-event-db ::change-message
(fn [db [_ message & more]]
(assoc db ::message [message more])))
(rf/reg-sub ::message
(fn [db]
(::message db)))
(defn app-view
[]
[:<>
[:p (str @(rf/subscribe [::message]))]
[:button
{:on-click #(rf/dispatch [::do-work-with-async-stuff])}
"do work"]
[:input
{:type :number
:value @(rf/subscribe [::delay])
:on-change #(rf/dispatch-sync [::change-delay (-> % .-target .-value)])}]
[:pre
(with-out-str (cljs.pprint/pprint @(rf/subscribe [::async-computed-results])))]])
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; re-frame setup
(defn- mount-app
[]
(rdom/render
[app-view]
(gdom/getElement "app")))
(defn ^:after-load on-reload
[]
(rf/clear-subscription-cache!)
(mount-app))
(defonce on-init
(mount-app))