-
Notifications
You must be signed in to change notification settings - Fork 1
/
cofx.cljc
64 lines (51 loc) · 1.75 KB
/
cofx.cljc
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
(ns prpr.a-frame.cofx
(:require
[promesa.core :as pr]
[prpr.error :as err]
[prpr.a-frame.schema :as schema]
[prpr.a-frame.registry :as registry]
[prpr.a-frame.cofx.data.tag-readers]
[prpr.a-frame.interceptor-chain :as interceptor-chain]
[taoensso.timbre :refer [info warn]]))
(defn reg-cofx
[id handler]
(registry/register-handler schema/a-frame-kind-cofx id handler))
(defn inject-cofx
"creates an InterceptorSpec data-structure for an event
interceptor chain"
([id]
{::interceptor-chain/key ::inject-cofx
::interceptor-chain/data
{::interceptor-chain/enter-data {::id id}}})
([id arg-spec]
{::interceptor-chain/key ::inject-cofx
::interceptor-chain/data
{::interceptor-chain/enter-data {::id id
::arg arg-spec}}}))
;; interceptor
(def inject-cofx-interceptor
"the interceptor functions to execute the interceptor
described by an InterceptorSpec produced by inject-cofx"
{::interceptor-chain/name ::inject-cofx
::interceptor-chain/enter
(fn inject-cofx-enter
[{app schema/a-frame-app-ctx
coeffects schema/a-frame-coeffects
:as context}
{id ::id
arg ::arg
:as data}]
(let [handler (registry/get-handler schema/a-frame-kind-cofx id)
has-arg? (contains? data ::arg)]
(if (some? handler)
(pr/let [coeffects' (if has-arg?
(handler app coeffects arg)
(handler app coeffects))]
(assoc context schema/a-frame-coeffects coeffects'))
(throw (err/ex-info
::no-cofx-handler
{::id id
::arg arg})))))})
(interceptor-chain/register-interceptor
::inject-cofx
inject-cofx-interceptor)