/
parser.cljc
601 lines (541 loc) · 24.5 KB
/
parser.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
(ns com.wsscode.pathom.parser
(:require [clojure.core.async :refer [go <!]]
[clojure.spec.alpha :as s]
[#?(:clj com.wsscode.async.async-clj
:cljs com.wsscode.async.async-cljs) :refer [<? <?maybe go-catch error? go-promise chan?]]
[clojure.core.async :as async]
[com.wsscode.pathom.misc :as p.misc]
[com.wsscode.pathom.trace :as pt :refer [trace tracing]]
[clojure.set :as set]
[com.fulcrologic.guardrails.core :refer [>def >defn >fdef => | <- ?]])
#?(:clj (:import (clojure.lang IDeref))))
(>def ::provides (s/coll-of (s/or :attr :com.wsscode.pathom.connect/attribute
:sym :com.wsscode.pathom.connect/sym
:ident :edn-query-language.core/ident) :kind set?))
(>def ::max-key-iterations int?)
(>def ::processing-recheck-timer (s/nilable pos-int?))
(>def ::external-wait-ignore-timeout (s/nilable pos-int?))
(declare expr->ast)
(defn- atom? [x]
#?(:clj (instance? IDeref x)
:cljs (satisfies? IDeref x)))
(defn- mark-meta [source target]
(cond-> target
(meta source) (assoc :meta (meta source))))
(defn symbol->ast [k]
{:dispatch-key k
:key k})
(defn keyword->ast [k]
{:type :prop
:dispatch-key k
:key k})
(defn union-entry->ast [[k v]]
(let [component (-> v meta :component)]
(merge
{:type :union-entry
:union-key k
:query v
:children (into [] (map expr->ast) v)}
(when-not (nil? component)
{:component component}))))
(defn union->ast [m]
{:type :union
:query m
:children (into [] (map union-entry->ast) m)})
(defn call->ast [[f args :as call]]
(if (= 'quote f)
(assoc (expr->ast args) :target (or (-> call meta :target) :remote))
(let [ast (update-in (expr->ast f) [:params] merge (or args {}))]
(cond-> (mark-meta call ast)
(symbol? (:dispatch-key ast)) (assoc :type :call)))))
(defn query->ast
"Convert a query to its AST representation."
[query]
(let [component (-> query meta :component)]
(merge
(mark-meta query
{:type :root
:children (into [] (map expr->ast) query)})
(when-not (nil? component)
{:component component}))))
(defn join->ast [join]
(let [query-root? (-> join meta :query-root)
[k v] (first join)
ast (expr->ast k)
type (if (= :call (:type ast)) :call :join)
component (-> v meta :component)]
(merge ast
(mark-meta join {:type type :query v})
(when-not (nil? component)
{:component component})
(when query-root?
{:query-root true})
(when-not (or (number? v) (= '... v))
(cond
(vector? v) {:children (into [] (map expr->ast) v)}
(map? v) {:children [(union->ast v)]}
:else (throw
(ex-info (str "Invalid join, " join)
{:type :error/invalid-join})))))))
(defn ident->ast [[k id :as ref]]
{:type :prop
:dispatch-key k
:key ref})
(defn expr->ast
"Given a query expression convert it into an AST."
[x]
(cond
(symbol? x) (symbol->ast x)
(keyword? x) (keyword->ast x)
(map? x) (join->ast x)
(vector? x) (ident->ast x)
(seq? x) (call->ast x)
:else (throw
(ex-info (str "Invalid expression " x)
{:type :error/invalid-expression}))))
(defn wrap-expr [root? expr]
(if root?
(with-meta
(cond-> expr (keyword? expr) list)
{:query-root true})
expr))
(defn parameterize [expr params]
(if-not (empty? params)
(list expr params)
(list expr)))
(defn ast->expr
"Given a query expression AST convert it back into a query expression."
([ast]
(ast->expr ast false))
([{:keys [type component] ast-meta :meta :as ast} unparse?]
(if (= :root type)
(cond-> (into (with-meta [] ast-meta) (map #(ast->expr % unparse?)) (:children ast))
(not (nil? component)) (vary-meta assoc :component component))
(let [{:keys [key query query-root params]} ast]
(wrap-expr query-root
(if (and params (not= :call type))
(let [expr (ast->expr (dissoc ast :params) unparse?)]
(parameterize expr params))
(let [key (if (= :call type) (parameterize key params) key)]
(if (or (= :join type)
(and (= :call type) (:children ast)))
(if (and (not= '... query) (not (number? query))
(or (true? unparse?)
(= :call type)))
(let [{:keys [children]} ast
query-meta (meta query)]
(if (and (== 1 (count children))
(= :union (:type (first children)))) ;; UNION
(with-meta
{key (into (cond-> (with-meta {} ast-meta)
component (vary-meta assoc :component component))
(map (fn [{:keys [union-key children component]}]
[union-key
(cond-> (into [] (map #(ast->expr % unparse?)) children)
(not (nil? component)) (vary-meta assoc :component component))]))
(:children (first children)))}
ast-meta)
(with-meta
{key (cond-> (into (with-meta [] query-meta) (map #(ast->expr % unparse?)) children)
(not (nil? component)) (vary-meta assoc :component component))}
ast-meta)))
(with-meta {key query} ast-meta))
key))))))))
(declare focus-subquery*)
(defn- focus-subquery-union*
[query-ast sub-ast]
(let [s-index (into {} (map #(vector (:union-key %) %)) (:children sub-ast))]
(assoc query-ast
:children
(reduce
(fn [children {:keys [union-key] :as union-entry}]
(if-let [sub (get s-index union-key)]
(conj children (focus-subquery* union-entry sub))
(conj children union-entry)))
[]
(:children query-ast)))))
(defn- focus-subquery*
[query-ast sub-ast]
(let [q-index (group-by :key (:children query-ast))]
(assoc query-ast
:children
(reduce
(fn [children {:keys [key type] :as focus}]
(if-let [source (get q-index key)]
(reduce
(fn [children source]
(cond
(= :join type (:type source))
(conj children (focus-subquery* source focus))
(= :union type (:type source))
(conj children (focus-subquery-union* source focus))
:else
(conj children source)))
children
source)
children))
[]
(:children sub-ast)))))
(defn focus-subquery
"Given a query, focus it along the specified query expression.
Examples:
(focus-query [:foo :bar :baz] [:foo])
=> [:foo]
(fulcro.client.primitives/focus-query [{:foo [:bar :baz]} :woz] [{:foo [:bar]} :woz])
=> [{:foo [:bar]} :woz]"
[query sub-query]
(let [query-ast (query->ast query)
sub-ast (query->ast sub-query)]
(ast->expr (focus-subquery* query-ast sub-ast) true)))
(defn normalize-atom [x] (if (atom? x) x (atom x)))
(defn ast->out-key [ast]
(or (get-in ast [:params :pathom/as])
(get ast :key)))
(defn parser [{:keys [read mutate]}]
(fn self [env tx]
(tracing env {::pt/event ::parse-loop}
(let [{:keys [children] :as tx-ast} (query->ast tx)
tx (vary-meta tx assoc ::ast tx-ast)
env (assoc env :parser self :com.wsscode.pathom.core/parent-query tx)]
(loop [res {}
[{:keys [query key type params] :as ast} & tail] children]
(if ast
(let [_ (trace env {::pt/event ::process-key :key key})
env (cond-> (merge env {:ast ast :query query})
(nil? query) (dissoc :query)
(= '... query) (assoc :query tx))
value (case type
:call
(do
(assert mutate "Parse mutation attempted but no :mutate function supplied")
(let [{:keys [action]} (mutate env key params)]
(if action
(action))))
(:prop :join :union)
(do
(assert read "Parse read attempted but no :read function supplied")
(read env))
nil)]
(recur (assoc res (ast->out-key ast) value) tail))
res))))))
(defn async-parser [{:keys [read mutate]}]
(fn self [env tx]
(go-catch
(tracing env {::pt/event ::parse-loop}
(let [{:keys [children] :as tx-ast} (query->ast tx)
tx (vary-meta tx assoc ::ast tx-ast)
env (assoc env :parser self :com.wsscode.pathom.core/parent-query tx)]
(loop [res {}
[{:keys [query key type params] :as ast} & tail] children]
(if ast
(let [_ (trace env {::pt/event ::process-key :key key})
env (cond-> (merge env {:ast ast :query query})
(nil? query) (dissoc :query)
(= '... query) (assoc :query tx))
value (case type
:call
(do
(assert mutate "Parse mutation attempted but no :mutate function supplied")
(let [{:keys [action]} (mutate env key params)]
(if action
(action))))
(:prop :join :union)
(do
(assert read "Parse read attempted but no :read function supplied")
(read env))
nil)
value (if (chan? value) (<? value) value)]
(recur
(assoc res (ast->out-key ast) value)
tail))
res)))))))
(defn watch-pending-key [{::keys [key-watchers external-wait-ignore-timeout]
:or {external-wait-ignore-timeout 3000}
:as env} key]
(let [ch (async/chan)]
(swap! key-watchers update key conj ch)
(go
; sometimes the watcher is too fast and finish the process before we get the change to register
; the watcher. This timeout ensures that in those cases we still flush out the watched key
(<! (async/timeout 1))
(when (contains? @(get env :com.wsscode.pathom.core/entity) key)
(trace env {::pt/event ::flush-watcher-safeguard :key key})
(async/put! ch {::provides #{key}})
(async/close! ch)))
(if external-wait-ignore-timeout
(go
(let [timer (async/timeout external-wait-ignore-timeout)
[res ch] (async/alts! [ch timer]
:priority true)]
(if (= ch timer)
(do
(pt/trace env {::pt/event ::watch-pending-timeout
::external-wait-ignore-timeout external-wait-ignore-timeout})
{::error ::watch-pending-timeout
::provides #{}})
res)))
ch)))
; urh, ugly copy from core but needed to avoid dep cycles
(defn- process-error [{:com.wsscode.pathom.core/keys [process-error] :as env} e]
(if process-error (process-error env e) e))
(defn- parallel-process-value [env tx ast
key-watchers
res waiting processing
read mutate key-iterations tail]
(let [{:keys [query key type params]} ast
env (cond-> (merge env {:ast ast
:query query
::waiting waiting
::key-watchers key-watchers})
(nil? query) (dissoc :query)
(= '... query) (assoc :query tx))
value (case type
:call
(do
(assert mutate "Parse mutation attempted but no :mutate function supplied")
(let [{:keys [action]} (mutate env key params)]
(if action
(go
(try
(trace env {::pt/event ::call-mutation
:mutation key})
(<?maybe (action))
(catch #?(:clj Throwable :cljs :default) e
{::error (process-error env e)}))))))
(:prop :join :union)
(do
(assert read "Parse read attempted but no :read function supplied")
(tracing env {::pt/event ::call-read :key key}
(read env)))
nil)]
(cond
(chan? value)
(let [provides #{key}
stream (go
{::provides provides
::merge-result? true
::response-value {key (<! value)
:pathom/as (ast->out-key ast)}})]
(trace env {::pt/event ::async-return
:key key})
[res
(into waiting provides)
(conj processing {::process-channel stream
::process-source ::channel-response
::provides provides})
key-iterations
tail])
(try
(::provides value)
(catch #?(:clj Throwable :cljs :default) _ nil))
(let [provides (::provides value)
stream (::response-stream value)]
(trace env {::pt/event ::provided-return
::provides provides})
[res
(into waiting provides)
(conj processing {::process-channel stream
::process-source ::parallel-reader
::provides provides})
key-iterations
tail])
:else
(do
(trace env {::pt/event ::value-return
:key key})
[(assoc res (ast->out-key ast) value) waiting processing key-iterations tail]))))
(defn- parallel-flush-watchers [env key-watchers provides error]
(pt/tracing env {::pt/event ::flush-watchers-loop}
(doseq [[pkey watchers] @key-watchers]
(when (contains? provides pkey)
(trace env {::pt/event ::flush-watchers
:key pkey
::watcher-count (count watchers)})
(doseq [out watchers]
(async/put! out {::provides provides
::error error})
(async/close! out))
(swap! key-watchers dissoc pkey)))))
(defn default-step-fn [amount min]
(fn [env x] (Math/max (- x amount) min)))
(defn remove-error-values [m]
(into {}
(remove (fn [[_ v]] (= v :com.wsscode.pathom.core/reader-error)))
m))
(defn value-merge
"This is used for merging new parsed attributes from entity, works like regular merge but if the value from the right
direction is not found, then the previous value will be kept."
[x y]
(if (or (identical? y :com.wsscode.pathom.core/reader-error)
(identical? y :com.wsscode.pathom.core/not-found))
x
y))
(defn process-next-message
[{::keys [done-signal* processing-recheck-timer active-paths]
:com.wsscode.pathom.core/keys [path]
:as env}
tx waiting indexed-props processing key-iterations key-watchers res]
(go-catch
(let [_ (trace env {::pt/event ::processing-wait-next
::processing-count (count processing)})
recheck-ch (if processing-recheck-timer (async/timeout processing-recheck-timer))
processing' (cond-> (into [] (map ::process-channel) processing)
recheck-ch
(conj recheck-ch))
[msg p] (async/alts! processing' :priority true)]
(if (= p recheck-ch)
(let [all-props (set (keys indexed-props))
current-props (set (keys res))
missing-props (set/difference all-props current-props)]
(pt/trace env {::pt/event ::trigger-reader-retry
::processing {:processes processing
:missing-props missing-props}})
(if (some #(contains? @active-paths (conj path %)) missing-props)
[res waiting processing key-iterations []]
(do
(pt/trace env {::pt/event ::trigger-recheck-schedule
::missing-props missing-props})
(doseq [{::keys [process-channel]} processing]
(async/close! process-channel))
(if @done-signal*
[res #{} #{} key-iterations []]
[res #{} #{} key-iterations (into [] (map indexed-props) missing-props)]))))
(let [{::keys [response-value provides merge-result? error]} msg
waiting' (::waiting msg)
provides' (set/difference provides waiting')
key-as (:pathom/as response-value)
response-value (dissoc response-value :pathom/as)
waiting (into waiting waiting')]
(if msg
(do
(trace env (cond-> {::pt/event ::process-pending
::provides provides
::response-value response-value
::merge-result? (boolean merge-result?)}
waiting' (assoc ::waiting waiting')))
(swap! (:com.wsscode.pathom.core/entity env) #(merge-with value-merge response-value %))
(parallel-flush-watchers env key-watchers provides' error)
(if merge-result?
(do
(pt/trace env {::pt/event ::merge-result ::response-value response-value})
[(assoc res key-as (first (vals response-value)))
(into #{} (remove provides') waiting)
processing
key-iterations
[]])
(let [next-children (->> (vec provides')
(focus-subquery tx)
(query->ast)
:children
(remove (comp (-> res remove-error-values keys set) ast->out-key))
(distinct))]
(pt/trace env {::pt/event ::reset-loop
::loop-keys (mapv :key next-children)})
[res
(into #{} (remove provides') waiting)
processing
key-iterations
next-children])))
[res waiting (into #{} (remove (comp #{p} ::process-channel)) processing) key-iterations []]))))))
(def zero-inc (fnil inc 0))
(defn call-parallel-parser
[{:keys [read mutate]}
{::keys [waiting key-watchers max-key-iterations
key-process-timeout key-process-timeout-step]
:com.wsscode.pathom.core/keys [entity-path-cache path]
:or {max-key-iterations 5}
:as env}
tx]
(go-catch
(let [key-process-timeout-step (or key-process-timeout-step (default-step-fn 1000 1000))
key-process-timeout (if key-process-timeout (key-process-timeout-step env key-process-timeout))
{:keys [children]} (query->ast tx)
key-watchers (or key-watchers (atom {}))
path-entity (get @entity-path-cache path {})
env (-> env
(assoc ::parallel? true
::key-process-timeout key-process-timeout)
(update :com.wsscode.pathom.core/entity
(fn [x]
(if (atom? x)
(do
(swap! x #(merge path-entity %))
x)
(atom (merge path-entity x))))))
indexed-props (into {} (map (fn [{:keys [key] :as ast}] [key ast])) children)]
(tracing env {::pt/event ::parse-loop
::key-process-timeout key-process-timeout}
(loop [res {}
waiting (or waiting #{})
processing #{}
key-iterations {}
[{:keys [key] :as ast} & tail] children]
(cond
; processing attributes
ast
(let [out-key (ast->out-key ast)]
(trace env {::pt/event ::process-key :key key})
(cond
(> (get key-iterations key 0) (dec max-key-iterations))
(do
(trace env {::pt/event ::max-iterations-reached :key key ::max-key-iterations max-key-iterations})
(recur (cond-> res
(not (contains? res out-key))
(assoc out-key :com.wsscode.pathom.core/not-found)) waiting processing key-iterations tail))
(and (contains? res out-key) (not= :com.wsscode.pathom.core/reader-error (get res out-key)))
(do
(trace env {::pt/event ::skip-resolved-key :key key})
(recur res waiting processing key-iterations tail))
(and (::key-watchers env)
(contains? waiting key))
(do
(trace env {::pt/event ::external-wait-key :key key})
(recur res waiting
(conj processing {::process-channel (watch-pending-key env key)
::process-source ::pending-key-watch
::provides #{key}})
key-iterations
tail))
(contains? waiting key)
(do
(trace env {::pt/event ::skip-wait-key :key key})
(recur res waiting processing key-iterations tail))
:else
(let [[res waiting processing key-iterations tail]
(parallel-process-value
env tx ast
key-watchers
res waiting processing
read mutate (update key-iterations out-key zero-inc) tail)]
(recur res waiting processing key-iterations tail))))
; waiting for results
(seq processing)
(let [[res waiting processing key-iterations tail] (<! (process-next-message env tx waiting indexed-props processing key-iterations key-watchers res))]
(recur res waiting processing key-iterations tail))
; done
:else
res))))))
(defn parallel-parser [{:keys [add-error] :as pconfig}]
(fn self [{::keys [key-process-timeout active-paths]
:com.wsscode.pathom.core/keys [path]
:or {key-process-timeout 60000}
:as env} tx]
(go-catch
(swap! active-paths conj path)
(let [res-ch (call-parallel-parser pconfig (assoc env :parser self
::key-process-timeout key-process-timeout
:com.wsscode.pathom.core/parent-query tx) tx)
channels (cond-> [res-ch] key-process-timeout (conj (async/timeout key-process-timeout)))
[res p] (async/alts! channels)]
(swap! active-paths disj path)
(if (= res-ch p)
res
(do
(trace env {::pt/event ::timeout-reach
::key-process-timeout key-process-timeout})
(add-error env (ex-info "Parallel read timeout" {:timeout key-process-timeout}))
{}))))))
(defn unique-ident?
#?(:cljs {:tag boolean})
[x]
(and (ident? x) (= '_ (second x))))