forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtojs.ts
More file actions
95 lines (80 loc) · 2.82 KB
/
tojs.ts
File metadata and controls
95 lines (80 loc) · 2.82 KB
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
import {
isObservable,
isObservableArray,
isObservableMap,
isObservableObject,
isObservableValue,
keys
} from "../internal"
export type ToJSOptions = {
detectCycles?: boolean
exportMapsAsObjects?: boolean
}
const defaultOptions: ToJSOptions = {
detectCycles: true,
exportMapsAsObjects: true
}
function cache<K, V>(map: Map<any, any>, key: K, value: V, options: ToJSOptions): V {
if (options.detectCycles) map.set(key, value)
return value
}
function toJSHelper(source, options: ToJSOptions, __alreadySeen: Map<any, any>) {
if (!isObservable(source)) return source
const detectCycles = options.detectCycles === true
if (
detectCycles &&
source !== null &&
typeof source === "object" &&
__alreadySeen.has(source)
) {
return __alreadySeen.get(source)
}
if (isObservableArray(source)) {
const res = cache(__alreadySeen, source, [] as any, options)
const toAdd = source.map(value => toJSHelper(value, options!, __alreadySeen))
res.length = toAdd.length
for (let i = 0, l = toAdd.length; i < l; i++) res[i] = toAdd[i]
return res
}
if (isObservableObject(source)) {
const res = cache(__alreadySeen, source, {}, options)
keys(source) // make sure we track the keys of the object
for (let key in source) {
res[key] = toJSHelper(source[key], options!, __alreadySeen)
}
return res
}
if (isObservableMap(source)) {
if (options.exportMapsAsObjects === false) {
const res = cache(__alreadySeen, source, new Map(), options)
source.forEach((value, key) => {
res.set(key, toJSHelper(value, options!, __alreadySeen))
})
return res
} else {
const res = cache(__alreadySeen, source, {}, options)
source.forEach((value, key) => {
res[key] = toJSHelper(value, options!, __alreadySeen)
})
return res
}
}
if (isObservableValue(source)) return toJSHelper(source.get(), options!, __alreadySeen)
return source
}
/**
* Basically, a deep clone, so that no reactive property will exist anymore.
*/
export function toJS<T>(source: T, options?: ToJSOptions): T
export function toJS(source: any, options?: ToJSOptions): any
export function toJS(source, options: ToJSOptions) // internal overload
export function toJS(source, options?: ToJSOptions) {
if (!isObservable(source)) return source
// backward compatibility
if (typeof options === "boolean") options = { detectCycles: options }
if (!options) options = defaultOptions
const detectCycles = options.detectCycles === true
let __alreadySeen
if (detectCycles) __alreadySeen = new Map()
return toJSHelper(source, options, __alreadySeen)
}