This repository has been archived by the owner on Jun 22, 2020. It is now read-only.
/
interceptor.cljs
70 lines (61 loc) · 1.71 KB
/
interceptor.cljs
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
(ns hipo.interceptor
(:require-macros hipo.interceptor))
(defprotocol Interceptor
(-intercept [this t m f]))
(defn call
[f v t m]
(let [i (first v)]
(-intercept i t m #(let [s (rest v)]
(if (seq s)
(call f s t m)
(f))))))
(deftype LogInterceptor [b]
Interceptor
(-intercept [_ t m f]
(if (or (not b) (not= :reconciliate t))
(.log js/console (name t) " " (clj->js m)))
(f)))
(deftype TimeInterceptor [s]
Interceptor
(-intercept [_ t _ f]
(let [label (str s "-" (name t))]
(.time js/console label)
(f)
(.timeEnd js/console label))))
(deftype ProfileInterceptor [label]
Interceptor
(-intercept [_ t _ f]
(when (= t :reconciliate)
(.profile js/console label)
(f)
(.profileEnd js/console label))
(f)))
(deftype PerformanceInterceptor [label]
Interceptor
; http://w3c.github.io/user-timing/
(-intercept [_ t _ f]
(let [mark-begin (str label " begin " t)
mark-end (str label " end " t)]
(.mark js/performance mark-begin)
(f)
(.mark js/performance mark-end)
(.measure js/performance (str label " " t) mark-begin mark-end))))
(deftype StateInterceptor [a]
Interceptor
(-intercept [_ t o f]
(swap! a #(cons %2 %1) {:type t :value o})
(f)))
(deftype StaticReconciliationInterceptor []
Interceptor
(-intercept [_ t o f]
(if (= :reconciliate t)
(if-not (contains? (meta (:new-value o)) :hipo/static)
(f))
(f))))
(deftype IdentityReconciliationInterceptor []
Interceptor
(-intercept [_ t o f]
(if (= :reconciliate t)
(if-not (identical? (:old-value o) (:new-value o))
(f))
(f))))