-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
entity.cljc
214 lines (181 loc) · 7.33 KB
/
entity.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
(ns ^:no-doc datascript.impl.entity
(:refer-clojure :exclude [keys get])
(:require [#?(:cljs cljs.core :clj clojure.core) :as c]
[datascript.db :as db]))
(declare entity ->Entity equiv-entity lookup-entity touch)
(defn- entid [db eid]
(when (or (number? eid)
(sequential? eid))
(db/entid db eid)))
(defn entity [db eid]
{:pre [(db/db? db)]}
(when-let [e (entid db eid)]
(->Entity db e (volatile! false) (volatile! {}))))
(defn- entity-attr [db a datoms]
(if (db/multival? db a)
(if (db/ref? db a)
(reduce #(conj %1 (entity db (:v %2))) #{} datoms)
(reduce #(conj %1 (:v %2)) #{} datoms))
(if (db/ref? db a)
(entity db (:v (first datoms)))
(:v (first datoms)))))
(defn- -lookup-backwards [db eid attr not-found]
(if-let [datoms (not-empty (db/-search db [nil attr eid]))]
(if (db/component? db attr)
(entity db (:e (first datoms)))
(reduce #(conj %1 (entity db (:e %2))) #{} datoms))
not-found))
#?(:cljs
(defn- multival->js [val]
(when val (to-array val))))
#?(:cljs
(defn- js-seq [e]
(touch e)
(for [[a v] @(.-cache e)]
(if (db/multival? (.-db e) a)
[a (multival->js v)]
[a v]))))
(deftype Entity [db eid touched cache]
#?@(:cljs
[Object
(toString [this]
(pr-str* this))
(equiv [this other]
(equiv-entity this other))
;; js/map interface
(keys [this]
(es6-iterator (c/keys this)))
(entries [this]
(es6-entries-iterator (js-seq this)))
(values [this]
(es6-iterator (map second (js-seq this))))
(has [this attr]
(not (nil? (.get this attr))))
(get [this attr]
(if (= attr ":db/id")
eid
(if (db/reverse-ref? attr)
(-> (-lookup-backwards db eid (db/reverse-ref attr) nil)
multival->js)
(cond-> (lookup-entity this attr)
(db/multival? db attr) multival->js))))
(forEach [this f]
(doseq [[a v] (js-seq this)]
(f v a this)))
(forEach [this f use-as-this]
(doseq [[a v] (js-seq this)]
(.call f use-as-this v a this)))
;; js fallbacks
(key_set [this] (to-array (c/keys this)))
(entry_set [this] (to-array (map to-array (js-seq this))))
(value_set [this] (to-array (map second (js-seq this))))
IEquiv
(-equiv [this o] (equiv-entity this o))
IHash
(-hash [_]
(hash eid)) ;; db?
ISeqable
(-seq [this]
(touch this)
(seq @cache))
ICounted
(-count [this]
(touch this)
(count @cache))
ILookup
(-lookup [this attr] (lookup-entity this attr nil))
(-lookup [this attr not-found] (lookup-entity this attr not-found))
IAssociative
(-contains-key? [this k]
(not= ::nf (lookup-entity this k ::nf)))
IFn
(-invoke [this k]
(lookup-entity this k))
(-invoke [this k not-found]
(lookup-entity this k not-found))
IPrintWithWriter
(-pr-writer [_ writer opts]
(-pr-writer (assoc @cache :db/id eid) writer opts))]
:clj
[Object
(toString [e] (pr-str (assoc @cache :db/id eid)))
(hashCode [e] (hash eid)) ; db?
(equals [e o] (equiv-entity e o))
clojure.lang.Seqable
(seq [e] (touch e) (seq @cache))
clojure.lang.Associative
(equiv [e o] (equiv-entity e o))
(containsKey [e k] (not= ::nf (lookup-entity e k ::nf)))
(entryAt [e k] (some->> (lookup-entity e k) (clojure.lang.MapEntry. k)))
(empty [e] (throw (UnsupportedOperationException.)))
(assoc [e k v] (throw (UnsupportedOperationException.)))
(cons [e [k v]] (throw (UnsupportedOperationException.)))
(count [e] (touch e) (count @(.-cache e)))
clojure.lang.ILookup
(valAt [e k] (lookup-entity e k))
(valAt [e k not-found] (lookup-entity e k not-found))
clojure.lang.IFn
(invoke [e k] (lookup-entity e k))
(invoke [e k not-found] (lookup-entity e k not-found))
]))
(defn entity? [x] (instance? Entity x))
#?(:clj
(defmethod print-method Entity [e, ^java.io.Writer w]
(.write w (str e))))
(defn- equiv-entity [^Entity this that]
(and
(instance? Entity that)
;; (= db (.-db ^Entity that))
(= (.-eid this) (.-eid ^Entity that))))
(defn- lookup-entity
([this attr] (lookup-entity this attr nil))
([^Entity this attr not-found]
(if (= attr :db/id)
(.-eid this)
(if (db/reverse-ref? attr)
(-lookup-backwards (.-db this) (.-eid this) (db/reverse-ref attr) not-found)
(if-some [v (@(.-cache this) attr)]
v
(if @(.-touched this)
not-found
(if-some [datoms (not-empty (db/-search (.-db this) [(.-eid this) attr]))]
(let [value (entity-attr (.-db this) attr datoms)]
(vreset! (.-cache this) (assoc @(.-cache this) attr value))
value)
not-found)))))))
(defn touch-components [db a->v]
(reduce-kv (fn [acc a v]
(assoc acc a
(if (db/component? db a)
(if (db/multival? db a)
(set (map touch v))
(touch v))
v)))
{} a->v))
(defn- datoms->cache [db datoms]
(reduce (fn [acc part]
(let [a (:a (first part))]
(assoc acc a (entity-attr db a part))))
{} (partition-by :a datoms)))
(defn touch [^Entity e]
{:pre [(entity? e)]}
(when-not @(.-touched e)
(when-let [datoms (not-empty (db/-search (.-db e) [(.-eid e)]))]
(vreset! (.-cache e) (->> datoms
(datoms->cache (.-db e))
(touch-components (.-db e))))
(vreset! (.-touched e) true)))
e)
#?(:cljs (do
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.get" (.-get (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.has" (.-has (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.forEach" (.-forEach (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.key_set" (.-key_set (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.value_set" (.-value_set (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.entry_set" (.-entry_set (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.keys" (.-keys (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.values" (.-values (.-prototype Entity)))
(goog/exportSymbol "datascript.impl.entity.Entity.prototype.entries" (.-entries (.-prototype Entity)))
(goog/exportSymbol "cljs.core.ES6Iterator.prototype.next" (.-next (.-prototype cljs.core/ES6Iterator)))
(goog/exportSymbol "cljs.core.ES6EntriesIterator.prototype.next" (.-next (.-prototype cljs.core/ES6EntriesIterator)))
))