-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.cljc
226 lines (176 loc) · 6.25 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
(ns prpr.a-frame.schema
(:require
[malli.util :as mu]
[prpr.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)
(def a-frame-events :a-frame/events)
(def a-frame-event-transitive-coeffects? :a-frame.event/transitive-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)
;; (s/defschema Event
;; [(s/one s/Keyword :event-kw)
;; s/Any])
(def Event
"an event as dispatched by applications"
[:cat :keyword [:* :any]])
;; (s/defschema Events
;; [Event])
(def Events
[:+ Event])
;; (s/defschema InitialCoeffects
;; {s/Keyword s/Any})
(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])
;; (s/defschema ExtendedEvent
;; {a-frame-event Event
;; (s/optional-key a-frame-coeffects) InitialCoeffects
;; (s/optional-key a-frame-event-transitive-coeffects?) s/Bool
;; (s/optional-key a-frame-event-modify-interceptor-chain) (s/pred fn?)})
(def ExtendedEvent
"an event along with any initial coeffects"
[:map
[a-frame-event Event]
[a-frame-coeffects {:optional true} InitialCoeffects]
[a-frame-event-transitive-coeffects? {:optional true} :boolean]
[a-frame-event-modify-interceptor-chain {:optional true} fn?]])
;; (s/defschema EventOrExtendedEvent
;; (s/conditional
;; vector? Event
;; map? ExtendedEvent))
(def EventOrExtendedEvent
[:or Event ExtendedEvent])
;; (s/defschema EventsOrExtendedEvents
;; [EventOrExtendedEvent])
(def EventsOrExtendedEvents
[:+ EventOrExtendedEvent])
;; (s/defschema Coeffects
;; {a-frame-coeffect-event Event
;; s/Keyword s/Any})
(def Coeffects
"these are the Coeffects that a handler will see"
[:map
[a-frame-coeffect-event Event]])
;; (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)
;; - strict? : 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"
;; ([strict? event-schema expected-coeffects-schema]
;; (prpr.s/merge-map-schemas
;; (if (some? event-schema)
;; {a-frame-coeffect-event event-schema}
;; {a-frame-coeffect-event Event})
;; expected-coeffects-schema
;; (when-not strict?
;; {s/Keyword s/Any})))
;; ([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)))
(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)))
;; (s/defschema EffectsMap
;; {s/Keyword s/Any})
(def EffectsMap
[:map-of :keyword :any])
(declare Effects)
;; (s/defschema EffectsVector
;; [Effects])
(def EffectsVector
[:sequential Effects])
;; (s/defschema Effects
;; (s/conditional
;; vector?
;; EffectsVector
;; map?
;; EffectsMap
;; nil?
;; (s/eq nil)))
(def Effects
[:or EffectsVector EffectsMap :nil])
;; (s/defschema AppCtx
;; {s/Keyword s/Any})
(def AppCtx
[:map-of :keyword :any])
;; (s/defschema Interceptor
;; {:id s/Keyword
;; (s/optional-key :enter) (s/pred fn?)
;; (s/optional-key :leave) (s/pred fn?)})
(def Interceptor
[:map
[:id :keyword]
[:enter {:optional true} :fn?]
[:leave {:optional true} :fn?]])
;; (s/defschema Router
;; {a-frame-app-ctx AppCtx
;; a-frame-router-event-stream #?(:clj (s/pred stream/stream?)
;; :cljs s/Any)
;; a-frame-router-global-interceptors-a s/Any
;; (s/optional-key a-frame-router-executor) s/Any
;; (s/optional-key a-frame-router-buffer-size) s/Int})
(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]])
;; (s/defschema HandleEventInterceptorCtx
;; {a-frame-router Router
;; a-frame-app-ctx AppCtx
;; a-frame-effects Effects
;; a-frame-coeffects Coeffects
;; s/Keyword s/Any})
(def HandleEventInterceptorCtx
[:map
[a-frame-router Router]
[a-frame-app-ctx AppCtx]
[a-frame-effects Effects]
[a-frame-coeffects Coeffects]])