This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
attachRenderer.js
221 lines (199 loc) · 7.39 KB
/
attachRenderer.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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
import type {DataType, OpaqueNodeHandle, Hook, ReactRenderer, Helpers} from './types';
var getData = require('./getData');
var getData012 = require('./getData012');
var attachRendererFiber = require('./attachRendererFiber');
type NodeLike = {};
/**
* This takes care of patching the renderer to emit events on the global
* `Hook`. The returned object has a `.cleanup` method to un-patch everything.
*/
function attachRenderer(hook: Hook, rid: string, renderer: ReactRenderer): Helpers {
var rootNodeIDMap = new Map();
var extras = {};
// Before 0.13 there was no Reconciler, so we patch Component.Mixin
var isPre013 = !renderer.Reconciler;
// React Fiber
if (typeof renderer.findFiberByHostInstance === 'function') {
return attachRendererFiber(hook, rid, renderer);
}
// React Native
if (renderer.Mount.findNodeHandle && renderer.Mount.nativeTagToRootNodeID) {
extras.getNativeFromReactElement = function(component) {
return renderer.Mount.findNodeHandle(component);
};
extras.getReactElementFromNative = function(nativeTag) {
var id = renderer.Mount.nativeTagToRootNodeID(nativeTag);
return rootNodeIDMap.get(id);
};
// React DOM 15+
} else if (renderer.ComponentTree) {
extras.getNativeFromReactElement = function(component) {
return renderer.ComponentTree.getNodeFromInstance(component);
};
extras.getReactElementFromNative = function(node) {
return renderer.ComponentTree.getClosestInstanceFromNode(node);
};
// React DOM
} else if (renderer.Mount.getID && renderer.Mount.getNode) {
extras.getNativeFromReactElement = function(component) {
try {
return renderer.Mount.getNode(component._rootNodeID);
} catch (e) {
return undefined;
}
};
extras.getReactElementFromNative = function(node) {
// $FlowFixMe
var id = renderer.Mount.getID(node);
while (node && node.parentNode && !id) {
node = node.parentNode;
// $FlowFixMe
id = renderer.Mount.getID(node);
}
return rootNodeIDMap.get(id);
};
} else {
console.warn('Unknown react version (does not have getID), probably an unshimmed React Native');
}
var oldMethods;
var oldRenderComponent;
var oldRenderRoot;
// React DOM
if (renderer.Mount._renderNewRootComponent) {
oldRenderRoot = decorateResult(renderer.Mount, '_renderNewRootComponent', (internalInstance) => {
hook.emit('root', {renderer: rid, internalInstance});
});
// React Native
} else if (renderer.Mount.renderComponent) {
oldRenderComponent = decorateResult(renderer.Mount, 'renderComponent', internalInstance => {
hook.emit('root', {renderer: rid, internalInstance: internalInstance._reactInternalInstance});
});
}
if (renderer.Component) {
console.error('You are using a version of React with limited support in this version of the devtools.\nPlease upgrade to use at least 0.13, or you can downgrade to use the old version of the devtools:\ninstructions here https://github.com/facebook/react-devtools/tree/devtools-next#how-do-i-use-this-for-react--013');
// 0.11 - 0.12
// $FlowFixMe renderer.Component is not "possibly undefined"
oldMethods = decorateMany(renderer.Component.Mixin, {
mountComponent() {
rootNodeIDMap.set(this._rootNodeID, this);
// FIXME DOMComponent calls Component.Mixin, and sets up the
// `children` *after* that call, meaning we don't have access to the
// children at this point. Maybe we should find something else to shim
// (do we have access to DOMComponent here?) so that we don't have to
// setTimeout.
setTimeout(() => {
hook.emit('mount', {internalInstance: this, data: getData012(this), renderer: rid});
}, 0);
},
updateComponent() {
setTimeout(() => {
hook.emit('update', {internalInstance: this, data: getData012(this), renderer: rid});
}, 0);
},
unmountComponent() {
hook.emit('unmount', {internalInstance: this, renderer: rid});
rootNodeIDMap.delete(this._rootNodeID);
},
});
} else if (renderer.Reconciler) {
oldMethods = decorateMany(renderer.Reconciler, {
mountComponent(internalInstance, rootID, transaction, context) {
var data = getData(internalInstance);
rootNodeIDMap.set(internalInstance._rootNodeID, internalInstance);
hook.emit('mount', {internalInstance, data, renderer: rid});
},
performUpdateIfNecessary(internalInstance, nextChild, transaction, context) {
hook.emit('update', {internalInstance, data: getData(internalInstance), renderer: rid});
},
receiveComponent(internalInstance, nextChild, transaction, context) {
hook.emit('update', {internalInstance, data: getData(internalInstance), renderer: rid});
},
unmountComponent(internalInstance) {
hook.emit('unmount', {internalInstance, renderer: rid});
rootNodeIDMap.delete(internalInstance._rootNodeID);
},
});
}
extras.walkTree = function(visit: (component: OpaqueNodeHandle, data: DataType) => void, visitRoot: (internalInstance: OpaqueNodeHandle) => void) {
var onMount = (component, data) => {
rootNodeIDMap.set(component._rootNodeID, component);
visit(component, data);
};
walkRoots(renderer.Mount._instancesByReactRootID || renderer.Mount._instancesByContainerID, onMount, visitRoot, isPre013);
};
extras.cleanup = function() {
if (oldMethods) {
if (renderer.Component) {
restoreMany(renderer.Component.Mixin, oldMethods);
} else {
restoreMany(renderer.Reconciler, oldMethods);
}
}
if (oldRenderRoot) {
renderer.Mount._renderNewRootComponent = oldRenderRoot;
}
if (oldRenderComponent) {
renderer.Mount.renderComponent = oldRenderComponent;
}
oldMethods = null;
oldRenderRoot = null;
oldRenderComponent = null;
};
extras.renderer = null;
return extras;
}
function walkRoots(roots, onMount, onRoot, isPre013) {
for (var name in roots) {
walkNode(roots[name], onMount, isPre013);
onRoot(roots[name]);
}
}
function walkNode(internalInstance, onMount, isPre013) {
var data = isPre013 ? getData012(internalInstance) : getData(internalInstance);
if (data.children && Array.isArray(data.children)) {
data.children.forEach(child => walkNode(child, onMount, isPre013));
}
onMount(internalInstance, data);
}
function decorateResult(obj, attr, fn) {
var old = obj[attr];
obj[attr] = function(instance: NodeLike) {
var res = old.apply(this, arguments);
fn(res);
return res;
};
return old;
}
function decorate(obj, attr, fn) {
var old = obj[attr];
obj[attr] = function(instance: NodeLike) {
var res = old.apply(this, arguments);
fn.apply(this, arguments);
return res;
};
return old;
}
function decorateMany(source, fns) {
var olds = {};
for (var name in fns) {
olds[name] = decorate(source, name, fns[name]);
}
return olds;
}
function restoreMany(source, olds) {
for (var name in olds) {
source[name] = olds[name];
}
}
module.exports = attachRenderer;