-
Notifications
You must be signed in to change notification settings - Fork 51
/
core.cljs
145 lines (111 loc) · 3.26 KB
/
core.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
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
(ns ueaq.core
(:require
[goog.object :as gobj]))
(defprotocol IUnwrappable
(-unwrap [o]))
(defn unkeywordize
[k]
(if-let [ns (namespace k)]
(str ns "/" (name k))
(name k)))
(defn unwrappable?
[p]
(satisfies? IUnwrappable p))
(defn unwrap
[p]
(if (satisfies? IUnwrappable p)
(-unwrap p)
(throw (js/Error. "Does not implement IUnwrappable"))))
(defn shallow-clj->js
[m]
(loop [entries (seq m)
o #js {}]
(if (nil? entries)
o
(let [entry (first entries)
k (key entry)
v (val entry)]
(recur
(next entries)
(doto o
(gobj/set (unkeywordize k) v)))))))
(declare ueaq)
;; -- traps
(defn getter
[o prop]
(this-as handler
(let [opts (.-opts ^js handler)
context (.-context ^js handler)
{:keys [recursive? prop->key]} opts
v (get o (prop->key prop)
(gobj/get context prop))]
(if (and recursive? (associative? v))
(ueaq v opts)
v))))
(defn has
[o prop]
(this-as handler
(let [{:keys [prop->key]} (.-opts ^js handler)]
(contains? o (prop->key prop)))))
(defn own-keys
[o]
(this-as handler
(let [{:keys [key->prop]} (.-opts ^js handler)]
(to-array (map key->prop (keys o))))))
(defn enumerate
[o]
(this-as handler
(let [{:keys [key->prop]} (.-opts ^js handler)]
(map key->prop (keys o)))))
(defn get-own-property-descriptor
[o prop]
(this-as handler
(let [{:keys [prop->key]} (.-opts ^js handler)
k (prop->key prop)]
(if (contains? o k)
#js {:enumerable true :configurable true
:writable false :value (get o k)}
js/undefined))))
(defn get-prototype-of
[_]
(this-as handler
(.-context ^js handler)))
(defn setter
[_ k v]
(this-as handler
(let [context (.-context ^js handler)]
(gobj/set context k v))))
(defn ^js ueaq
([o] (ueaq o {}))
([o {:keys [recursive? prop->key key->prop] :as opts
:or {recursive? false
prop->key keyword
key->prop unkeywordize}}]
(let [;; this is an object to hold implementations of various protocols for
;; CLJS usage
context (specify! #js {}
;; Object
;; (toString [this]
;; (pr-str* this))
IUnwrappable
(-unwrap [_] o)
;; IPrintWithWriter
;; (-pr-writer [_ writer _]
;; ;; prn is not a fast path
;; (-write writer
;; (if recursive?
;; (pr-str (clj->js o))
;; (pr-str (shallow-clj->js o)))))
)
handler #js {:opts (assoc opts
:prop->key prop->key
:key->prop key->prop)
:context context
:get getter
:has has
:ownKeys own-keys
:enumerate enumerate
:getOwnPropertyDescriptor get-own-property-descriptor
:getPrototypeOf get-prototype-of
:set setter}]
(js/Proxy. o handler))))