-
Notifications
You must be signed in to change notification settings - Fork 50
/
createClassProxy.js
268 lines (228 loc) · 7.46 KB
/
createClassProxy.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import find from 'lodash/find';
import createPrototypeProxy from './createPrototypeProxy';
import bindAutoBindMethods from './bindAutoBindMethods';
import deleteUnknownAutoBindMethods from './deleteUnknownAutoBindMethods';
import supportsProtoAssignment from './supportsProtoAssignment';
const RESERVED_STATICS = [
'length',
'displayName',
'name',
'arguments',
'caller',
'prototype',
'toString'
];
function isEqualDescriptor(a, b) {
if (!a && !b) {
return true;
}
if (!a || !b) {
return false;
}
for (let key in a) {
if (a[key] !== b[key]) {
return false;
}
}
return true;
}
function getDisplayName(Component) {
const displayName = Component.displayName || Component.name;
return (displayName && displayName !== 'ReactComponent') ?
displayName :
'Unknown';
}
// This was originally a WeakMap but we had issues with React Native:
// https://github.com/gaearon/react-proxy/issues/50#issuecomment-192928066
let allProxies = [];
function findProxy(Component) {
const pair = find(allProxies, ([key]) => key === Component);
return pair ? pair[1] : null;
}
function addProxy(Component, proxy) {
allProxies.push([Component, proxy]);
}
function proxyClass(InitialComponent) {
// Prevent double wrapping.
// Given a proxy class, return the existing proxy managing it.
var existingProxy = findProxy(InitialComponent);
if (existingProxy) {
return existingProxy;
}
let CurrentComponent;
let ProxyComponent;
let savedDescriptors = {};
function instantiate(factory, context, params) {
const component = factory();
try {
return component.apply(context, params);
} catch (err) {
const instance = new component(...params);
Object.keys(instance).forEach(key => {
if (RESERVED_STATICS.indexOf(key) > -1) {
return;
}
context[key] = instance[key];
})
}
}
let displayName = getDisplayName(InitialComponent);
try {
// Create a proxy constructor with matching name
ProxyComponent = new Function('factory', 'instantiate',
`return function ${displayName}() {
return instantiate(factory, this, arguments);
}`
)(() => CurrentComponent, instantiate);
} catch (err) {
// Some environments may forbid dynamic evaluation
ProxyComponent = function () {
return instantiate(() => CurrentComponent, this, arguments);
};
}
try {
Object.defineProperty(ProxyComponent, 'name', {
value: displayName
});
} catch (err) { }
// Proxy toString() to the current constructor
ProxyComponent.toString = function toString() {
return CurrentComponent.toString();
};
let prototypeProxy;
if (InitialComponent.prototype && InitialComponent.prototype.isReactComponent) {
// Point proxy constructor to the proxy prototype
prototypeProxy = createPrototypeProxy();
ProxyComponent.prototype = prototypeProxy.get();
}
function update(NextComponent) {
if (typeof NextComponent !== 'function') {
throw new Error('Expected a constructor.');
}
if (NextComponent === CurrentComponent) {
return;
}
// Prevent proxy cycles
var existingProxy = findProxy(NextComponent);
if (existingProxy) {
return update(existingProxy.__getCurrent());
}
// Save the next constructor so we call it
const PreviousComponent = CurrentComponent;
CurrentComponent = NextComponent;
// Try to infer displayName
displayName = getDisplayName(NextComponent);
ProxyComponent.displayName = displayName;
try {
Object.defineProperty(ProxyComponent, 'name', {
value: displayName
});
} catch (err) { }
// Set up the same prototype for inherited statics
ProxyComponent.__proto__ = NextComponent.__proto__;
// Copy over static methods and properties added at runtime
if (PreviousComponent) {
Object.getOwnPropertyNames(PreviousComponent).forEach(key => {
if (RESERVED_STATICS.indexOf(key) > -1) {
return;
}
const prevDescriptor = Object.getOwnPropertyDescriptor(PreviousComponent, key);
const savedDescriptor = savedDescriptors[key];
if (!isEqualDescriptor(prevDescriptor, savedDescriptor)) {
try {
Object.defineProperty(NextComponent, key, prevDescriptor);
} catch (err) { }
}
});
}
// Copy newly defined static methods and properties
Object.getOwnPropertyNames(NextComponent).forEach(key => {
if (RESERVED_STATICS.indexOf(key) > -1) {
return;
}
const prevDescriptor = PreviousComponent && Object.getOwnPropertyDescriptor(PreviousComponent, key);
const savedDescriptor = savedDescriptors[key];
// Skip redefined descriptors
if (prevDescriptor && savedDescriptor && !isEqualDescriptor(savedDescriptor, prevDescriptor)) {
try {
Object.defineProperty(NextComponent, key, prevDescriptor);
Object.defineProperty(ProxyComponent, key, prevDescriptor);
} catch (err) { }
return;
}
if (prevDescriptor && !savedDescriptor) {
Object.defineProperty(ProxyComponent, key, prevDescriptor);
return;
}
const nextDescriptor = {
...Object.getOwnPropertyDescriptor(NextComponent, key),
configurable: true
};
savedDescriptors[key] = nextDescriptor;
Object.defineProperty(ProxyComponent, key, nextDescriptor);
});
// Remove static methods and properties that are no longer defined
Object.getOwnPropertyNames(ProxyComponent).forEach(key => {
if (RESERVED_STATICS.indexOf(key) > -1) {
return;
}
// Skip statics that exist on the next class
if (NextComponent.hasOwnProperty(key)) {
return;
}
// Skip non-configurable statics
const proxyDescriptor = Object.getOwnPropertyDescriptor(ProxyComponent, key);
if (proxyDescriptor && !proxyDescriptor.configurable) {
return;
}
const prevDescriptor = PreviousComponent && Object.getOwnPropertyDescriptor(PreviousComponent, key);
const savedDescriptor = savedDescriptors[key];
// Skip redefined descriptors
if (prevDescriptor && savedDescriptor && !isEqualDescriptor(savedDescriptor, prevDescriptor)) {
return;
}
delete ProxyComponent[key];
});
if (prototypeProxy) {
// Update the prototype proxy with new methods
const mountedInstances = prototypeProxy.update(NextComponent.prototype);
// Set up the constructor property so accessing the statics work
ProxyComponent.prototype.constructor = NextComponent;
// We might have added new methods that need to be auto-bound
mountedInstances.forEach(bindAutoBindMethods);
mountedInstances.forEach(deleteUnknownAutoBindMethods);
}
};
function get() {
return ProxyComponent;
}
function getCurrent() {
return CurrentComponent;
}
update(InitialComponent);
const proxy = { get, update };
addProxy(ProxyComponent, proxy);
Object.defineProperty(proxy, '__getCurrent', {
configurable: false,
writable: false,
enumerable: false,
value: getCurrent
});
return proxy;
}
function createFallback(Component) {
let CurrentComponent = Component;
return {
get() {
return CurrentComponent;
},
update(NextComponent) {
CurrentComponent = NextComponent;
}
};
}
export default function createClassProxy(Component) {
return Component.__proto__ && supportsProtoAssignment() ?
proxyClass(Component) :
createFallback(Component);
}