Skip to content

Commit

Permalink
Add unpack interceptor
Browse files Browse the repository at this point in the history
See #644
  • Loading branch information
superstructor committed Sep 7, 2020
1 parent 9638bbf commit 52fb9c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/re_frame/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
[re-frame.registrar :as registrar]
[re-frame.interceptor :as interceptor]
[re-frame.std-interceptors :as std-interceptors :refer [db-handler->interceptor
fx-handler->interceptor
ctx-handler->interceptor]]
fx-handler->interceptor
ctx-handler->interceptor]]
[re-frame.utils :as utils]
[clojure.set :as set]))

Expand Down Expand Up @@ -674,6 +674,21 @@
[f]
(std-interceptors/enrich f))

(def ^{:api-docs/heading "Interceptors"} unpack
"An interceptor which provides the second element of the event vector to the
event handler as the event, intended to be used with the second element as a
map.
Your event handlers will look like this:
(reg-event-fx
:event-id
[... unpack ...] ;; <-- added to the interceptors
(fn [{:keys [db]} {:keys [x y z]}] ;; <-- instead of [_ {:keys [x y z]}]
...)
"
std-interceptors/unpack)

(def ^{:api-docs/heading "Interceptors"} trim-v
"An interceptor which removes the first element of the event vector,
before it is supplied to the event handler, allowing you to write more
Expand Down
19 changes: 17 additions & 2 deletions src/re_frame/std_interceptors.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,31 @@
(console :log "No app-db changes resulted from:" event))))
context))))

(def unpack
(->interceptor
:id :unpack
:before (fn unpack-before
[context]
(let [[event-id payload :as event] (get-coeffect context :event)]
(-> context
(assoc-coeffect :event (with-meta payload {:event-id event-id}))
(assoc-coeffect ::packed-event event))))
:after (fn unpack-after
[context]
(-> context
(utils/dissoc-in [:coeffects ::packed-event])
(assoc-coeffect :event (get-coeffect context ::packed-event))))))


(def trim-v
(->interceptor
:id :trim-v
:before (fn trimv-before
:before (fn trim-v-before
[context]
(-> context
(update-coeffect :event subvec 1)
(assoc-coeffect ::untrimmed-event (get-coeffect context :event))))
:after (fn trimv-after
:after (fn trim-v-after
[context]
(-> context
(utils/dissoc-in [:coeffects ::untrimmed-event])
Expand Down

0 comments on commit 52fb9c4

Please sign in to comment.