forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifiers.ts
More file actions
52 lines (44 loc) · 1.6 KB
/
Copy pathmodifiers.ts
File metadata and controls
52 lines (44 loc) · 1.6 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
import {
deepEqual,
fail,
isES6Map,
isObservable,
isObservableArray,
isObservableMap,
isObservableObject,
isPlainObject,
observable
} from "../internal"
export interface IEnhancer<T> {
(newValue: T, oldValue: T | undefined, name: string): T
}
export function deepEnhancer(v, _, name) {
// it is an observable already, done
if (isObservable(v)) return v
// something that can be converted and mutated?
if (Array.isArray(v)) return observable.array(v, { name })
if (isPlainObject(v)) return observable.object(v, undefined, { name })
if (isES6Map(v)) return observable.map(v, { name })
return v
}
export function shallowEnhancer(v, _, name): any {
if (v === undefined || v === null) return v
if (isObservableObject(v) || isObservableArray(v) || isObservableMap(v)) return v
if (Array.isArray(v)) return observable.array(v, { name, deep: false })
if (isPlainObject(v)) return observable.object(v, undefined, { name, deep: false })
if (isES6Map(v)) return observable.map(v, { name, deep: false })
return fail(
process.env.NODE_ENV !== "production" &&
"The shallow modifier / decorator can only used in combination with arrays, objects and maps"
)
}
export function referenceEnhancer(newValue?) {
// never turn into an observable
return newValue
}
export function refStructEnhancer(v, oldValue, name): any {
if (process.env.NODE_ENV !== "production" && isObservable(v))
throw `observable.struct should not be used with observable values`
if (deepEqual(v, oldValue)) return oldValue
return v
}