-
Notifications
You must be signed in to change notification settings - Fork 8
/
map.cljc
229 lines (179 loc) · 5.04 KB
/
map.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
(ns ribelo.doxa.map
(:require
[ribelo.extropy :as ex]
[ribelo.doxa.util :as u]
[ribelo.doxa.protocols :as p])
#?(:clj
(:import
(java.util Map))))
#?(:clj (set! *warn-on-reflection* true))
#?(:clj
(deftype Doxa [db cache_ index tx listeners_]
Object
(equals [_ obj] (.equals db obj))
(hashCode [_] (.hashCode db))
(toString [_] (.toString db))
Map
Iterable
(iterator [_] (.iterator ^Iterable db))
clojure.lang.Counted
clojure.lang.Seqable
(seq [_] (.seq ^clojure.lang.Seqable db))
clojure.lang.IPersistentCollection
(count [_] (.count ^clojure.lang.IPersistentCollection db))
(equiv [_ o] (.equiv ^clojure.lang.IPersistentCollection db o))
clojure.lang.ILookup
(valAt [_ o] (.valAt ^clojure.lang.ILookup db o))
(valAt [_ o not-found] (.valAt ^clojure.lang.ILookup db o not-found))
clojure.lang.Associative
(assoc [_ _ _]
(throw (ex-info "not suported" {})))
clojure.lang.IReduce
(reduce [_ f]
(reduce f db))
clojure.lang.IReduceInit
(reduce [_ f v]
(reduce f v db))
clojure.lang.IKVReduce
(kvreduce [_ f init]
(reduce-kv f init db))
clojure.lang.IFn
(invoke [_ k]
(.valAt ^clojure.lang.ILookup db k))
(invoke [_ k not-found]
(.valAt ^clojure.lang.ILookup db k not-found))
p/IDoxa
(p/-put [_ e m]
(let [ndb (assoc db e m)
oe (get db e {})
ne (get ndb e {})
diff (u/-diff-entity oe ne)]
(Doxa. ndb cache_ index (into tx diff) listeners_)))
(p/-del [_ e]
(let [ndb (dissoc db e e)
oe (get db e {})
diff (u/-diff-entity oe {})]
(Doxa. ndb cache_ index (into tx diff) listeners_)))
(p/-connect [this] (atom this))
(p/-listen! [_ k f]
(swap! listeners_ assoc k f)
(fn [] (dissoc @listeners_ k)))
(p/-unlisten! [_ k]
(dissoc @listeners_ k))
(p/-tx [_] tx)
(p/-clear-tx [_]
(Doxa. db cache_ index [] listeners_))
(p/-cache [_]
cache_)
(p/-set-cache! [this cache]
(reset! cache_ cache)
this)
(p/-index [_] index)
(p/-reindex [_]
(Doxa. db cache_ (u/-update-index index tx) tx listeners_))))
#?(:cljs
(deftype Doxa [db cache_ index tx listeners_]
Object
(equiv [_ other] (-equiv db other))
(toString [_] (.toString db))
(keys [_] (keys db))
(vals [_] (vals db))
IPrintWithWriter
(-pr-writer [_ writer opts]
(-pr-writer db writer opts))
IWithMeta
(-with-meta [this meta]
(if (identical? meta (-meta db))
this
(Doxa. (-with-meta db meta) cache_ index tx listeners_)))
IMeta
(-meta [_]
(-meta db))
IMap
ICounted
(-count [_]
(-count db))
IIterable
(-iterator [_]
(-iterator db))
ISeqable
(-seq [_] (-seq db))
IEquiv
(-equiv [_ other]
(-equiv db other))
ILookup
(-lookup [_ k]
(-lookup db k))
(-lookup [_ k not-found]
(-lookup db k not-found))
IAssociative
(-contains-key? [_ k]
(-contains-key? db k))
(-assoc [_ _ _]
(throw (ex-info "unsuported operation" {})))
ISeq
(-first [_]
(-first db))
(-rest [_]
(-rest db))
IReduce
(-reduce [_ f ]
(-reduce db f))
(-reduce [_ f init]
(-reduce db f init))
IKVReduce
(-kv-reduce [_ f init]
(-kv-reduce db f init))
IHash
(-hash [_]
(-hash db))
IFn
(-invoke [_ k]
(-lookup db k))
(-invoke [_ k not-found]
(-lookup db k not-found))
p/IDoxa
(p/-put [_ e m]
(let [ndb (assoc db e m)
oe (get db e {})
ne (get ndb e {})
diff (u/-diff-entity oe ne)]
(Doxa. ndb cache_ index (into tx diff) listeners_)))
(p/-del [_ e]
(let [ndb (dissoc db e e)
oe (get db e {})
diff (u/-diff-entity oe {})]
(Doxa. ndb cache_ index (into tx diff) listeners_)))
(p/-connect [this] (atom this))
(p/-listen! [_ k f]
(swap! listeners_ assoc k f)
(fn [] (dissoc @listeners_ k)))
(p/-unlisten! [_ k]
(dissoc @listeners_ k))
(p/-tx [_] tx)
(p/-clear-tx [_]
(Doxa. db cache_ index [] listeners_))
(p/-cache [_]
cache_)
(p/-set-cache! [this cache]
(reset! cache_ cache)
this)
(p/-index [_]
index)
(p/-reindex [_]
(Doxa. db cache_ (u/-update-index index tx) tx listeners_))
))
(defn empty-db
([] (empty-db {}))
([{:keys [cache]}]
(Doxa. {} (atom cache) {} [] (atom {}))))
#?(:cljs
(def transit-writer-handler
{Doxa
(reify Object
(tag [_ _] "dx/map")
(rep [_ ^js dx] (js/Array (.-db dx) (empty @(.-cache_ dx)) (.-index dx)))
(stringRep [_ _] nil)
(verboseHandler [_] nil))}))
(def -transit-read-handler
{"dx/map" (fn [[m cache index]] (Doxa. m (atom cache) index [] (atom {})))})