-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.cljc
155 lines (120 loc) · 4.15 KB
/
schema.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
(ns a-frame.schema
(:require
[malli.util :as mu]
[promisespromises.stream :as stream]))
(def a-frame-state :a-frame/state)
(def a-frame-state-handlers-a :a-frame.state/handlers-a)
(def a-frame-router :a-frame/router)
(def a-frame-router-event-stream :a-frame.router/event-stream)
(def a-frame-router-executor :a-frame.router/executor)
(def a-frame-router-buffer-size :a-frame.router/buffer-size)
(def a-frame-router-global-interceptors :a-frame.router/global-interceptors)
(def a-frame-router-global-interceptors-a :a-frame.router/global-interceptors-a)
;; a system-map
(def a-frame-app-ctx :a-frame/app-ctx)
;; a timbre with-context map for logging
(def a-frame-log-ctx :a-frame/log-ctx)
(def a-frame-event :a-frame/event) ;; an event
(def a-frame-id :a-frame/id) ;; an event id
;;
(def a-frame-events :a-frame/events)
(def a-frame-event-transitive-coeffects? :a-frame.event/transitive-coeffects?)
(def a-frame-init-coeffects :a-frame.event/init-coeffects)
(def a-frame-event-modify-interceptor-chain
:a-frame.event/modify-interceptor-chain)
(def a-frame-interceptor-init-ctx :a-frame.events.interceptor/init-ctx)
(def a-frame-effects :a-frame/effects)
(def a-frame-coeffects :a-frame/coeffects)
(def a-frame-coeffect-event :a-frame.coeffect/event)
(def a-frame-kind-fx :a-frame.kind/fx)
(def a-frame-kind-cofx :a-frame.kind/cofx)
;; the event-handler interceptor chain
(def a-frame-kind-event :a-frame.kind/event)
;; the pure event handler
(def a-frame-kind-event-pure :a-frame.kind/event-pure)
(def InitialCoeffects
"these are initial coeffects which can be given to a dispatch
they don't yet have the Event associated with them"
[:map-of :keyword :any])
(def MapEvent
"an event as dispatched by applications"
[:map
;; mandatory id field
[a-frame-id :keyword]
;; open to any other keys as required by an event handler
])
(def VectorEvent
[:cat :keyword [:* :any]])
(def Event
[:or MapEvent VectorEvent])
(def EventOptions
"an event along with some options affecting event processing"
[:map
{:closed true}
;; the event
[a-frame-event Event]
;; options to direct event processing
[a-frame-init-coeffects {:optional true} InitialCoeffects]
[a-frame-event-transitive-coeffects? {:optional true} :boolean]
[a-frame-event-modify-interceptor-chain {:optional true} fn?]])
(def EventOrEventOptions
[:or Event EventOptions])
(def Events
"a list of events"
[:+ Event])
(def EventOrEventOptionsList
[:+ EventOrEventOptions])
(def Coeffects
"the Coeffects that a handler will see"
[:map
[a-frame-coeffect-event Event]
;; open to other keys
])
(defn derive-coeffects-schema
"given some expected coeffects, derive
a Coeffects schema from the base schema
(always including the event, with a lax schema if no
stricter schema is given)
- closed? : true to prevent additional coeffect keys. false by default
- event-schema : a more restrictive schema for the event
- expected-coeffects-schema : a map schema for exepcted coeffects"
([closed? event-schema expected-coeffects-schema]
(cond->
(mu/merge
[:map
[a-frame-coeffect-event
(if (some? event-schema)
event-schema
Event)]]
expected-coeffects-schema)
closed? (mu/closed-schema)))
([expected-coeffects-schema]
(derive-coeffects-schema false nil expected-coeffects-schema))
([event-schema expected-coeffects-schema]
(derive-coeffects-schema false event-schema expected-coeffects-schema)))
(def EffectsMap
[:map-of :keyword :any])
(def EffectsVector
[:* EffectsMap])
(def Effects
[:or EffectsVector EffectsMap :nil])
(def AppCtx
[:map-of :keyword :any])
(def Interceptor
[:map
[:id :keyword]
[:enter {:optional true} fn?]
[:leave {:optional true} fn?]])
(def Router
[:map
[a-frame-app-ctx AppCtx]
[a-frame-router-event-stream [:fn stream/stream?]]
[a-frame-router-global-interceptors-a :any]
[a-frame-router-executor {:optional true} :any]
[a-frame-router-buffer-size {:optional true} :int]])
(def HandleEventInterceptorCtx
[:map
[a-frame-router Router]
[a-frame-app-ctx AppCtx]
[a-frame-effects Effects]
[a-frame-coeffects Coeffects]])