Skip to content

Commit

Permalink
Supports user task id and filtering by task id instead of task
Browse files Browse the repository at this point in the history
- Fixes readme and example code
  • Loading branch information
jtkDvlp committed Nov 19, 2020
1 parent 1f4282c commit 16a3310
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,7 @@ Interceptor and helpers to register and unregister (background-)tasks (FXs) in y
* register / unregister tasks / fxs via one line interceptor injection
* support multiple and any fx on-completion keys
* subscriptions for tasks list and running task boolean
* running task boolean can be quick filtered by task id
* events to register / unregister tasks yourself
* helpers to register / unregister tasks into db yourself

Expand All @@ -31,32 32,41 @@ Add the following dependency to your `project.clj`:<br>


(rf/reg-event-fx :some-event
;; give it the fx to identity the task
;; give it the fx to identify the task emitted by this event
[(tasks/as-task :some-fx)
;; of course you can use this interceptor more for than one fx in a event call
;; of course you can use this interceptor for more than one fx in a event call
;; futher more you can give it the handler keys to hang in finishing the task
(tasks/as-task :some-other-fx :on-done :on-done-with-errors)]
(fn [_ _]
{:some-fx
{,,,
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
::tasks/id :some-important-stuff
:label "Do some fx"
:on-success [:some-event-success]
:on-error [:some-event-error]
;; Calling this by `:some-fx` will unregister the task
;; calling this by `:some-fx` will unregister the task via `tasks/as-task`
:on-completed [:some-event-completed]}

:some-other-fx
{,,,
:label "Do some other fx"
;; Calling this by some-fx will unregister the task
;; 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]}}))

(defn app-view
[]
(let [block-ui?
;; for sugar you can give it also a pred (called with the task id) e.g. a set of task ids to filter the running tasks.
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running?])

any-running-task?
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running?])

some-important-stuff-running?
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running? #{:some-important-stuff}])

tasks
(rf/subscribe [:jtk-dvlp.re-frame.tasks/tasks])]

Expand All @@ -65,7 75,7 @@ Add the following dependency to your `project.clj`:<br>
[:div "some app content"]

[:ul "task list"
;; each task is the original fx map plus an `::tasks/id`
;; each task is the original fx map plus an `::tasks/id` and `::tasks/effect`
(for [{:keys [label :jtk-dvlp.re-frame.tasks/id] :as _task} @tasks]
^{:key id}
[:li label])]
Expand Down
19 changes: 14 additions & 5 deletions dev/jtk_dvlp/your_project.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 5,41 @@


(rf/reg-event-fx :some-event
;; give it the fx to identity the task
;; give it the fx to identify the task emitted by this event
[(tasks/as-task :some-fx)
;; of course you can use this interceptor more for than one fx in a event call
;; of course you can use this interceptor for more than one fx in a event call
;; futher more you can give it the handler keys to hang in finishing the task
(tasks/as-task :some-other-fx :on-done :on-done-with-errors)]
(fn [_ _]
{:some-fx
{,,,
;; you can give the tasks an id (default: uuid), see subscription `:jtk-dvlp.re-frame.tasks/running?` for usage.
::tasks/id :some-important-stuff
:label "Do some fx"
:on-success [:some-event-success]
:on-error [:some-event-error]
;; Calling this by `:some-fx` will unregister the task via `tasks/as-task`
;; calling this by `:some-fx` will unregister the task via `tasks/as-task`
:on-completed [:some-event-completed]}

:some-other-fx
{,,,
:label "Do some other fx"
;; Calling this by some-fx will unregister the task via `tasks/as-task`
;; 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]}}))

(defn app-view
[]
(let [block-ui?
;; for sugar you can give it also a pred (called with the task id) e.g. a set of task ids to filter the running tasks.
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running?])

any-running-task?
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running?])

some-important-stuff-running?
(rf/subscribe [:jtk-dvlp.re-frame.tasks/running? #{:some-important-stuff}])

tasks
(rf/subscribe [:jtk-dvlp.re-frame.tasks/tasks])]

Expand All @@ -39,7 48,7 @@
[:div "some app content"]

[:ul "task list"
;; each task is the original fx map plus an `::tasks/id`
;; each task is the original fx map plus an `::tasks/id` and `::tasks/effect`
(for [{:keys [label :jtk-dvlp.re-frame.tasks/id] :as _task} @tasks]
^{:key id}
[:li label])]
Expand Down
16 changes: 8 additions & 8 deletions src/jtk_dvlp/re_frame/tasks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 35,14 @@

:after
(fn [context]
(let [task-id
(random-uuid)

effect
(let [effect
(rf/get-effect context effect-key)

task-id
(::id effect (random-uuid))

task
(assoc effect ::id task-id)
(assoc effect ::id task-id, ::effect effect-key)

effect'
(->> on-completion-keys
Expand Down Expand Up @@ -94,7 94,7 @@

(rf/reg-sub ::running?
:<- [::tasks]
(fn [tasks [_ pred]]
(if pred
(->> tasks (filter pred) (some?))
(fn [tasks [_ ids]]
(if ids
(->> tasks (keys) (filter ids) (seq) (some?))
(->> tasks (seq) (some?)))))

0 comments on commit 16a3310

Please sign in to comment.