-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
transit.cljs
53 lines (44 loc) · 1.37 KB
/
transit.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
(ns fulcro.inspect.remote.transit
(:require [cognitect.transit :as t]
[com.cognitect.transit.types :as ty]
[fulcro.transit :as ft]
[clojure.walk :as walk]))
(deftype ErrorHandler []
Object
(tag [this v] "js-error")
(rep [this v] [(ex-message v) (ex-data v)])
(stringRep [this v] (ex-message v)))
(def write-handlers
{cljs.core/ExceptionInfo (ErrorHandler.)})
(def read-handlers
{"js-error" (fn [[msg data]] (ex-info msg data))})
(defn read [str]
(let [reader (ft/reader {:handlers read-handlers})]
(t/read reader str)))
(defn sanitize-fns [x]
(walk/prewalk
(fn [x]
(if (fn? x)
"(fn ...)"
x))
x))
(defn sanitize [writer x]
(let [warned (atom #{})]
(walk/postwalk
(fn [x]
(try
(t/write writer x)
x
(catch :default _
(when-not (contains? @warned x)
(js/console.warn "Fulcro inspect failed to encode" x "\nThis means Fulcro Inspect had to walk your data to sanitize information, remove non serializable values on your app db to avoid slow encodings.")
(swap! warned conj x))
(pr-str x))))
x)))
(defn write [x]
(let [writer (ft/writer {:handlers write-handlers})]
(try
(t/write writer x)
(catch :default _
(t/write writer (sanitize writer x))))))
(extend-type ty/UUID IUUID)