-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
tempid.cljc
55 lines (47 loc) · 1.19 KB
/
tempid.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
(ns fulcro.tempid
#?(:clj (:import [java.io Writer])))
;; =============================================================================
;; ClojureScript
#?(:cljs
(deftype TempId [^:mutable id ^:mutable __hash]
Object
(toString [this]
(pr-str this))
IEquiv
(-equiv [this other]
(and (instance? TempId other)
(= (. this -id) (. other -id))))
IHash
(-hash [this]
(when (nil? __hash)
(set! __hash (hash id)))
__hash)
IPrintWithWriter
(-pr-writer [_ writer _]
(write-all writer "#fulcro/tempid[\"" id "\"]"))))
#?(:cljs
(defn tempid
([]
(tempid (random-uuid)))
([id]
(TempId. id nil))))
;; =============================================================================
;; Clojure
#?(:clj
(defrecord TempId [id]
Object
(toString [this]
(pr-str this))))
#?(:clj
(defmethod print-method TempId [^TempId x ^Writer writer]
(.write writer (str "#fulcro/tempid[\"" (.id x) "\"]"))))
#?(:clj
(defn tempid
([]
(tempid (java.util.UUID/randomUUID)))
([uuid]
(TempId. uuid))))
(defn tempid?
#?(:cljs {:tag boolean})
[x]
(instance? TempId x))