/
resolver.js
85 lines (67 loc) · 2.13 KB
/
resolver.js
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
import { createProxyForType, getProxyByType, isProxyType, isTypeBlacklisted } from './proxies';
import {
getComponentDisplayName,
isCompositeComponent,
isContextType,
isForwardType,
isLazyType,
isMemoType,
} from '../internal/reactUtils';
import configuration, { internalConfiguration } from '../configuration';
const shouldNotPatchComponent = type => isTypeBlacklisted(type);
export function resolveUtility(type) {
// all "utility" types are resolved to their __initial__ shapes
// that enables to never change reference to them, and gives the ability to maintain React Tree on HMR
// all operations could be skipped with react-hot-dom enabled
if (typeof type === 'object') {
if (configuration.integratedComparator) {
return type;
}
const element = { type };
if (isLazyType(element) || isMemoType(element) || isForwardType(element) || isContextType(element)) {
return getProxyByType(type) || type;
}
}
return undefined;
}
export function resolveComponent(type, options = {}) {
const existingProxy = getProxyByType(type);
// cold API
if (shouldNotPatchComponent(type)) {
return existingProxy ? existingProxy.getCurrent() : type;
}
if (!existingProxy && configuration.onComponentCreate) {
configuration.onComponentCreate(type, getComponentDisplayName(type));
if (shouldNotPatchComponent(type)) {
return type;
}
}
const proxy = internalConfiguration.disableProxyCreation ? existingProxy : createProxyForType(type, options);
return proxy ? proxy.get() : undefined;
}
export function resolveProxy(type) {
if (isProxyType(type)) {
return type;
}
return undefined;
}
export function resolveNotComponent(type) {
if (!isCompositeComponent(type)) {
return type;
}
return undefined;
}
export const resolveSimpleType = type => {
if (!type) {
return type;
}
return resolveProxy(type) || resolveUtility(type) || type;
};
export const resolveType = (type, options = {}) => {
if (!type) {
return type;
}
return (
resolveProxy(type) || resolveUtility(type) || resolveNotComponent(type) || resolveComponent(type, options) || type
);
};