Skip to content

Commit

Permalink
Simplifies / betters api, fixes bugs
Browse files Browse the repository at this point in the history
- Betters completion-keys api
- Adds helpers for original-event management of unregister task event
- Fixes fx special handling
- Betters example script
  • Loading branch information
jtkDvlp committed Oct 23, 2022
1 parent 9b5f220 commit 812ac26
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 138 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Interceptor and helpers to register and unregister (background-)tasks (FXs) in y
## Features

* register / unregister tasks / fxs via one line interceptor injection
* support multiple and any fx on-completion keys
* support multiple and any fx on-completion keys via registration
* subscriptions for tasks list and running task boolean
* running task boolean can be quick filtered by task name
* events to register / unregister tasks yourself
* helpers to register / unregister tasks into db yourself

Also works for async coeffects injections, see https://github.com/jtkDvlp/re-frame-async-coeffects.
Also works for async coeffect injections, see https://github.com/jtkDvlp/re-frame-async-coeffects.

## Getting started

Expand All @@ -36,38 +36,34 @@ See api docs [![cljdoc badge](https://cljdoc.org/badge/jtk-dvlp/re-frame-tasks)]


(rf/reg-event-fx :some-event
;; give it the fx to identify the task emitted by this event
[(tasks/as-task :some-task
[:some-fx
[:some-other-fx :on-done :on-done-with-errors]
[[:fx 1] :on-done]])
;; give it a name and the fxs to monitor
[(tasks/as-task :some-task [:some-fx [:fx 1]])
;; supports async coeffects
(acoeffects/inject-acofx :some-acofx)]
(fn [_ _]
(println "handler")
{;; modify your task via fx
::tasks/task
{:this-is-some-task :data}

:some-fx
{,,,
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
:on-success [:some-event-success]
{:on-success [:some-event-success]
:on-error [:some-event-error]
;; calling this by `:some-fx` will unregister the task via `tasks/as-task`
:on-complete [:some-event-completed]}
:on-complete [:some-event-completed]
,,,}

:some-other-fx
{,,,
;; calling this by some-fx will unregister the task via `tasks/as-task`
;; `:on-done-with-error` will also untergister the task when called by `:some-other-fx`
:on-done [:some-other-event-completed]}
{:on-done [:some-other-event-completed]
,,,}

:fx
[[:some-fx
{:on-success [:some-event-success]
:on-error [:some-event-error]
:on-complete [:some-event-completed]}]
:on-complete [:some-event-completed]
,,,}]

;; same with :fx effect referenzed via path [:fx 1]
;; monitored fx referenzed via path [:fx 1]
[:some-other-fx
{:on-done [:some-other-event-completed]}]]}))

Expand All @@ -80,20 +76,25 @@ See api docs [![cljdoc badge](https://cljdoc.org/badge/jtk-dvlp/re-frame-tasks)]
(rf/subscribe [:jtk-dvlp.re-frame.tasks/tasks])]

(fn []
[:p "open developer tools console for more infos."]

[:<>
[:button {:on-click #(rf/dispatch [:some-event])}
"exec some event"]
[:button {:on-click #(rf/dispatch [:some-bad-event])}
"exec some bad event"]
[:button {:on-click #(rf/dispatch [:some-other-bad-event])}
"exec some other bad event"]

[:ul "task list " (count @tasks)
;; each task is the original fx map plus an `::tasks/id`, the original `event`
;; and the data you carry via `::task` fx from within the event
;; task is a map of `::tasks/id`, `:name`, `:event and the
;; data you carry via `::task` fx from within the event
(for [{:keys [::tasks/id] :as task} @tasks]
^{:key id}
[:li [:pre (with-out-str (cljs.pprint/pprint task))]])]

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

```

## Appendix
Expand Down
81 changes: 34 additions & 47 deletions dev/jtk_dvlp/your_project.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
(<! (timeout 5000))
:result))

(tasks/set-completion-keys-per-effect!
{:some-fx #{:on-complete}
:some-other-fx #{:on-done :on-done-with-errors}})

(rf/reg-fx :some-fx
(fn [{:keys [on-complete] :as x}]
(println ":some-fx" x)
Expand Down Expand Up @@ -57,11 +61,9 @@
result))))

(rf/reg-event-fx :some-event
;; give it the fx to identify the task emitted by this event
[(tasks/as-task :some-task
[:some-fx
[:some-other-fx :on-done :on-done-with-errors]
[[:fx 1] :on-done]])
;; give it a name and the fxs to monitor
[(tasks/as-task :some-task [:some-fx [:fx 1]])
;; supports async coeffects
(acoeffects/inject-acofx :some-acofx)]
(fn [_ _]
(println "handler")
Expand All @@ -70,75 +72,58 @@
{:this-is-some-task :data}

:some-fx
{,,,
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
:on-success [:some-event-success]
{:on-success [:some-event-success]
:on-error [:some-event-error]
;; calling this by `:some-fx` will unregister the task via `tasks/as-task`
:on-complete [:some-event-completed]}
:on-complete [:some-event-completed]
,,,}

:some-other-fx
{,,,
;; calling this by some-fx will unregister the task via `tasks/as-task`
;; `:on-done-with-error` will also untergister the task when called by `:some-other-fx`
:on-done [:some-other-event-completed]}
{:on-done [:some-other-event-completed]
,,,}

:fx
[[:some-fx
{:on-success [:some-event-success]
:on-error [:some-event-error]
:on-complete [:some-event-completed]}]
:on-complete [:some-event-completed]
,,,}]

;; same with :fx effect referenzed via path [:fx 1]
;; monitored fx referenzed via path [:fx 1]
[:some-other-fx
{:on-done [:some-other-event-completed]}]]}))

;; error case, error within async coeffect
(rf/reg-event-fx :some-bad-event
;; give it the fx to identify the task emitted by this event
[(tasks/as-task :some-task
[:some-fx
{:effect-key [:some-other-fx]
:completion-keys #{:on-done :on-done-with-errors}}])
[(tasks/as-task :some-task [:some-fx :some-other-fx])
(acoeffects/inject-acofx [:some-acofx :bad-data])]
(fn [_ _]
(println "handler")
{:some-fx
{,,,
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
:on-success [:some-event-success]
{:on-success [:some-event-success]
:on-error [:some-event-error]
;; calling this by `:some-fx` will unregister the task via `tasks/as-task`
:on-complete [:some-event-completed]}
:on-complete [:some-event-completed]
,,,}

:some-other-fx
{,,,
;; calling this by some-fx will unregister the task via `tasks/as-task`
;; `:on-done-with-error` will also untergister the task when called by `:some-other-fx`
:on-done [:some-other-event-completed]}}))
{:on-done [:some-other-event-completed]
,,,}}))

;; error case, error within effect
(rf/reg-event-fx :some-other-bad-event
;; give it the fx to identify the task emitted by this event
[(tasks/as-task :some-task
[:some-fx
{:effect-key [:some-other-fx]
:completion-keys #{:on-done :on-done-with-errors}}])
[(tasks/as-task :some-task [:some-fx :some-other-fx])
(acoeffects/inject-acofx :some-acofx)]
(fn [_ _]
(println "handler")
{:some-fx
{,,,
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
:on-success [:some-event-success]
{:on-success [:some-event-success]
:on-error [:some-event-error]
;; calling this by `:some-fx` will unregister the task via `tasks/as-task`
:on-complete [:some-event-completed]}
:on-complete [:some-event-completed]
,,,}

:some-other-fx
{,,,
:bad-data true
;; calling this by some-fx will unregister the task via `tasks/as-task`
;; `:on-done-with-error` will also untergister the task when called by `:some-other-fx`
:on-done [:some-other-event-completed]}}))
{:bad-data true
:on-done [:some-other-event-completed]
,,,}}))

(rf/reg-event-db :some-event-completed
(fn [db _]
Expand All @@ -157,6 +142,8 @@
(rf/subscribe [:jtk-dvlp.re-frame.tasks/tasks])]

(fn []
[:p "open developer tools console for more infos."]

[:<>
[:button {:on-click #(rf/dispatch [:some-event])}
"exec some event"]
Expand All @@ -166,8 +153,8 @@
"exec some other bad event"]

[:ul "task list " (count @tasks)
;; each task is the original fx map plus an `::tasks/id`, the original `event`
;; and the data you carry via `::task` fx from within the event
;; task is a map of `::tasks/id`, `:name`, `:event and the
;; data you carry via `::task` fx from within the event
(for [{:keys [::tasks/id] :as task} @tasks]
^{:key id}
[:li [:pre (with-out-str (cljs.pprint/pprint task))]])]
Expand Down
Loading

0 comments on commit 812ac26

Please sign in to comment.