-
Notifications
You must be signed in to change notification settings - Fork 124
/
cursor.cljs
59 lines (46 loc) · 1.16 KB
/
cursor.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
(ns rum.cursor)
(deftype Cursor [ref path meta]
Object
(equiv [this other]
(-equiv this other))
IAtom
IMeta
(-meta [_] meta)
IEquiv
(-equiv [this other]
(identical? this other))
IDeref
(-deref [_]
(get-in (-deref ref) path))
IWatchable
(-add-watch [this key callback]
(add-watch ref (list this key)
(fn [_ _ oldv newv]
(let [old (get-in oldv path)
new (get-in newv path)]
(when (not= old new)
(callback key this old new)))))
this)
(-remove-watch [this key]
(remove-watch ref (list this key))
this)
IHash
(-hash [this] (goog/getUid this))
IReset
(-reset! [_ newv]
(swap! ref assoc-in path newv)
newv)
ISwap
(-swap! [this f]
(-reset! this (f (-deref this))))
(-swap! [this f a]
(-reset! this (f (-deref this) a)))
(-swap! [this f a b]
(-reset! this (f (-deref this) a b)))
(-swap! [this f a b rest]
(-reset! this (apply f (-deref this) a b rest)))
IPrintWithWriter
(-pr-writer [this writer opts]
(-write writer "#object [rum.cursor.Cursor ")
(pr-writer {:val (-deref this)} writer opts)
(-write writer "]")))