Skip to content

Commit 216200d

Browse files
committed
Betters usability of task data
- Changes name to a non namespaced keyword - Adds ability to modify the task from within the event - Betters event unregister-and-dispatch-original to work with manual registered tasks - Tasks subscriptions returns tasks vector instead of map
1 parent 8e00aae commit 216200d

File tree

3 files changed

+70
-42
lines changed

3 files changed

+70
-42
lines changed

README.md

+35-25
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Interceptor and helpers to register and unregister (background-)tasks (FXs) in y
1515
* events to register / unregister tasks yourself
1616
* helpers to register / unregister tasks into db yourself
1717

18-
Alsoe works for async coeffects injections, see https://github.com/jtkDvlp/re-frame-async-coeffects.
18+
Also works for async coeffects injections, see https://github.com/jtkDvlp/re-frame-async-coeffects.
1919

2020
## Getting started
2121

@@ -36,50 +36,60 @@ See api docs [![cljdoc badge](https://cljdoc.org/badge/jtk-dvlp/re-frame-tasks)]
3636

3737

3838
(rf/reg-event-fx :some-event
39-
;; give it a name and fx keys to identify the task and effects emitted by this event.
40-
[(tasks/as-task :some-task [:some-fx :some-other-fx])
41-
;; futher more you can give it the handler keys to hang in finishing the tasks effects
42-
;; (tasks/as-task :some-task
43-
;; [[:some-fx :on-done :on-done-with-errors]
44-
;; [:some-other-fx :on-yuhu :on-oh-no]])
45-
;; last but not least supports the special effect :fx giving an path for the fx to monitor.
46-
;; (tasks/as-task :some-task [[[:fx 1] :on-done ,,,] ; you need to give it the index of the effect within :fx vector to monitor.
47-
;; ,,,])
48-
]
39+
;; give it the fx to identify the task emitted by this event
40+
[(tasks/as-task :some-task
41+
[:some-fx
42+
[:some-other-fx :on-done :on-done-with-errors]
43+
[[:fx 1] :on-done]])
44+
(acoeffects/inject-acofx :some-acofx)]
4945
(fn [_ _]
50-
{:some-fx
51-
{:label "Do some fx"
46+
{;; modify your task via fx
47+
::tasks/task
48+
{:this-is-some-task :data}
49+
50+
:some-fx
51+
{,,,
52+
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
5253
:on-success [:some-event-success]
5354
:on-error [:some-event-error]
54-
:on-done [:some-event-completed]}
55+
;; calling this by `:some-fx` will unregister the task via `tasks/as-task`
56+
:on-complete [:some-event-completed]}
5557

5658
:some-other-fx
5759
{,,,
58-
:label "Do some other fx"
60+
;; calling this by some-fx will unregister the task via `tasks/as-task`
5961
;; `:on-done-with-error` will also untergister the task when called by `:some-other-fx`
60-
:on-done [:some-other-event-completed]}}))
62+
:on-done [:some-other-event-completed]}
63+
64+
:fx
65+
[[:some-fx
66+
{:on-success [:some-event-success]
67+
:on-error [:some-event-error]
68+
:on-complete [:some-event-completed]}]
69+
70+
;; same with :fx effect referenzed via path [:fx 1]
71+
[:some-other-fx
72+
{:on-done [:some-other-event-completed]}]]}))
6173

6274
(defn app-view
6375
[]
6476
(let [block-ui?
6577
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running?])
6678

67-
;; for sugar you can give it also a task name to filter the running tasks.
68-
some-important-stuff-running?
69-
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running? :some-important-stuff])
70-
7179
tasks
7280
(rf/subscribe [:jtk-dvlp.re-frame.tasks/tasks])]
7381

7482
(fn []
7583
[:<>
76-
[:div "some app content"]
84+
[:button {:on-click #(rf/dispatch [:some-event])}
85+
"exec some event"]
7786

78-
[:ul "task list"
79-
;; each task is a map with the original event vector plus name and id.
80-
(for [{:keys [:jtk-dvlp.re-frame.tasks/id] :as task} @tasks]
87+
[:ul "task list " (count @tasks)
88+
;; each task is the original fx map plus an `::tasks/id`, the original `event`
89+
;; and the data you carry via `::task` fx from within the event
90+
(for [{:keys [::tasks/id] :as task} @tasks]
8191
^{:key id}
82-
[:li task])]
92+
[:li [:pre (with-out-str (cljs.pprint/pprint task))]])]
8393

8494
(when @block-ui?
8595
[:div "this div blocks the UI if there are running tasks"])])))

dev/jtk_dvlp/your_project.cljs

+8-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@
6565
(acoeffects/inject-acofx :some-acofx)]
6666
(fn [_ _]
6767
(println "handler")
68-
{:some-fx
68+
{;; modify your task via fx
69+
::tasks/task
70+
{:this-is-some-task :data}
71+
72+
:some-fx
6973
{,,,
7074
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
7175
:on-success [:some-event-success]
@@ -162,8 +166,9 @@
162166
"exec some other bad event"]
163167

164168
[:ul "task list " (count @tasks)
165-
;; each task is the original fx map plus an `::tasks/id` and `::tasks/effect`
166-
(for [{:keys [::tasks/id] :as task} (vals @tasks)]
169+
;; each task is the original fx map plus an `::tasks/id`, the original `event`
170+
;; and the data you carry via `::task` fx from within the event
171+
(for [{:keys [::tasks/id] :as task} @tasks]
167172
^{:key id}
168173
[:li [:pre (with-out-str (cljs.pprint/pprint task))]])]
169174

src/jtk_dvlp/re_frame/tasks.cljs

+27-14
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
[name-or-task]
4747
(if (map? name-or-task)
4848
name-or-task
49-
{::name name-or-task}))
49+
{:name name-or-task}))
5050

5151
(defn- normalize-effect-key
5252
[effect-key]
@@ -143,12 +143,14 @@
143143
{:keys [dispatch-id ?error]}
144144
acoeffects
145145

146-
task
146+
{task-id ::id :as task}
147147
(assoc task ::id dispatch-id)]
148148

149149
(if (fx-handler-run? context)
150150
(or
151-
(unregister-by-fxs context task fxs)
151+
(-> context
152+
(interceptor/assoc-effect :db (update-in db [::db :tasks task-id] merge task))
153+
(unregister-by-fxs task fxs))
152154
(interceptor/assoc-effect context :db (unregister db task)))
153155
(-> context
154156
(interceptor/assoc-effect :db (register db task))
@@ -180,11 +182,13 @@
180182

181183
(defn as-task
182184
"Creates an interceptor to mark an event as task.
183-
Give it a name of the task or map with at least a `::name` key or nil / nothing to use the event name.
185+
Give it a name of the task or map with at least a `:name` key or nil / nothing to use the event name.
184186
Tasks can be used via subscriptions `::tasks` and `::running?`.
185187
186188
Given vector `fxs` will be used to identify effects to monitor for the task. Can be the keyword of the effect or an vector of effect keyword or effect path (to handle special :fx effect) and completion keywords to hang in. Completion keys defaults to `:on-complete`, `:on-success`, `on-failure` and `on-error`. See also `set-global-default-completion-keys!` and `merge-global-default-completion-keys!`.
187189
190+
Within your event handler use `::task` as effect to modify your task data.
191+
188192
Works in combination with https://github.com/jtkDvlp/re-frame-async-coeffects. For async coeffects there is no need to define what to monitor. Coeffects will be monitored automatically."
189193
([]
190194
(as-task nil))
@@ -204,7 +208,12 @@
204208
(-> name-or-task
205209
(or (task-by-original-event context))
206210
(normalize-task)
207-
(assoc ::event (get-original-event context)))]
211+
(assoc :event (get-original-event context))
212+
(merge (interceptor/get-effect context ::task)))
213+
214+
;; NOTE: ::task fx is only to carry task data
215+
context
216+
(update context :effects dissoc ::task)]
208217

209218
(cond
210219
(includes-acofxs? context)
@@ -234,11 +243,13 @@
234243
(when original-event-vec
235244
(rf/dispatch (into original-event-vec original-event-args)))
236245

237-
(if (= 1 (get @!task<->fxs-counters id))
238-
(do
239-
(swap! !task<->fxs-counters dissoc id)
240-
(rf/dispatch [::unregister task]))
241-
(swap! !task<->fxs-counters update id dec))))
246+
(if-let [fxs-rest-count (get @!task<->fxs-counters id)]
247+
(if (= 1 fxs-rest-count)
248+
(do
249+
(swap! !task<->fxs-counters dissoc id)
250+
(rf/dispatch [::unregister task]))
251+
(swap! !task<->fxs-counters update id dec))
252+
(rf/dispatch [::unregister task]))))
242253

243254

244255
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -251,11 +262,13 @@
251262
(rf/reg-sub ::tasks
252263
:<- [::db]
253264
(fn [{:keys [tasks]}]
254-
tasks))
265+
(vals tasks)))
255266

256267
(rf/reg-sub ::running?
257268
:<- [::tasks]
258269
(fn [tasks [_ name]]
259-
(if name
260-
(->> tasks (vals) (filter #(= (::name %) name)) (first) (some?))
261-
(-> tasks (first) (some?)))))
270+
(-> tasks
271+
(cond->>
272+
name (some #(= (:name %) name)))
273+
(seq)
274+
(some?))))

0 commit comments

Comments
 (0)