/
proxyAdapter.js
161 lines (144 loc) · 4.47 KB
/
proxyAdapter.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from 'react';
import { enterHotUpdate, get as getGeneration, hotComparisonOpen, setComparisonHooks } from '../global/generation';
import { getProxyByType, setStandInOptions } from './proxies';
import reconcileHotReplacement, { flushScheduledUpdates, unscheduleUpdate } from './index';
import configuration, { internalConfiguration } from '../configuration';
import { EmptyErrorPlaceholder, logException } from '../errorReporter';
import { RENDERED_GENERATION } from '../proxy';
export const renderReconciler = (target, force) => {
// we are not inside parent reconcilation
const currentGeneration = getGeneration();
const componentGeneration = target[RENDERED_GENERATION];
target[RENDERED_GENERATION] = currentGeneration;
if (!internalConfiguration.disableProxyCreation) {
if ((componentGeneration || force) && componentGeneration !== currentGeneration) {
enterHotUpdate();
reconcileHotReplacement(target);
return true;
}
}
return false;
};
function asyncReconciledRender(target) {
renderReconciler(target, false);
}
export function proxyWrapper(element) {
// post wrap on post render
if (!internalConfiguration.disableProxyCreation) {
unscheduleUpdate(this);
}
if (!element) {
return element;
}
if (Array.isArray(element)) {
return element.map(proxyWrapper);
}
if (typeof element.type === 'function') {
const proxy = getProxyByType(element.type);
if (proxy) {
return {
...element,
type: proxy.get(),
};
}
}
return element;
}
const ERROR_STATE = 'react_hot_loader_catched_error';
const ERROR_STATE_PROTO = 'react_hot_loader_catched_error-prototype';
const OLD_RENDER = 'react_hot_loader_original_render';
function componentDidCatch(error, errorInfo) {
this[ERROR_STATE] = {
location: 'boundary',
error,
errorInfo,
generation: getGeneration(),
};
Object.getPrototypeOf(this)[ERROR_STATE_PROTO] = this[ERROR_STATE];
if (!configuration.errorReporter) {
logException(error, errorInfo, this);
}
this.forceUpdate();
}
function componentRender(...args) {
const { error, errorInfo, generation } = this[ERROR_STATE] || {};
if (error && generation === getGeneration()) {
return React.createElement(configuration.errorReporter || EmptyErrorPlaceholder, {
error,
errorInfo,
component: this,
});
}
if (this.hotComponentUpdate) {
this.hotComponentUpdate();
}
try {
return this[OLD_RENDER].render.call(this, ...args);
} catch (renderError) {
this[ERROR_STATE] = {
location: 'render',
error: renderError,
generation: getGeneration(),
};
if (!configuration.errorReporter) {
logException(renderError, undefined, this);
}
return componentRender.call(this);
}
}
export function retryHotLoaderError() {
delete this[ERROR_STATE];
this.forceUpdate();
}
setComparisonHooks(
() => ({}),
component => {
if (!hotComparisonOpen()) {
return;
}
const { prototype } = component;
if (!prototype[OLD_RENDER]) {
const renderDescriptior = Object.getOwnPropertyDescriptor(prototype, 'render');
prototype[OLD_RENDER] = {
descriptor: renderDescriptior ? renderDescriptior.value : undefined,
render: prototype.render,
};
prototype.componentDidCatch = componentDidCatch;
prototype.retryHotLoaderError = retryHotLoaderError;
prototype.render = componentRender;
}
delete prototype[ERROR_STATE];
},
({ prototype }) => {
if (prototype[OLD_RENDER]) {
const { generation } = prototype[ERROR_STATE_PROTO] || {};
if (generation === getGeneration()) {
// still in error.
// keep render hooked
} else {
delete prototype.componentDidCatch;
delete prototype.retryHotLoaderError;
// undo only what we did
if (prototype.render === componentRender) {
if (!prototype[OLD_RENDER].descriptor) {
delete prototype.render;
} else {
prototype.render = prototype[OLD_RENDER].descriptor;
}
} else {
console.error('React-Hot-Loader: something unexpectedly mutated Component', prototype);
}
delete prototype[ERROR_STATE_PROTO];
delete prototype[OLD_RENDER];
}
}
},
);
setStandInOptions({
componentWillRender: asyncReconciledRender,
componentDidRender: proxyWrapper,
componentDidUpdate: component => {
component[RENDERED_GENERATION] = getGeneration();
flushScheduledUpdates();
},
});