@samitouri / QOS-React-2 / commits / 65ac53bba4

Fixes

Dan Abramov committed Jun 7, 2019 at 18:33 UTC 65ac53bba41031c313e38ca55509b5e436f668dc
1 file changed +53 -25
src/backend/legacy/renderer.js
+53 -25
@@ -27,10 +27,7 @@ import type {
27 RendererInterface,
28 } from '../types';
29 import type { ComponentFilter, ElementType } from 'src/types';
30 -import type {
31 - InspectedElement,
32 - Owner,
33 -} from 'src/devtools/views/Components/types';
30 +import type { Owner, InspectedElement } from '../types';
31
32 export type InternalInstance = Object;
33 type LegacyRenderer = Object;
@@ -87,7 +84,14 @@ export function attach(
84 global: Object
85 ): RendererInterface {
86 const idToInternalInstanceMap: Map<number, InternalInstance> = new Map();
90 - const internalInstanceToIDMap: Map<InternalInstance, number> = new Map();
87 + const internalInstanceToIDMap: WeakMap<
88 + InternalInstance,
89 + number
90 + > = new WeakMap();
91 + const internalInstanceToRootIDMap: WeakMap<
92 + InternalInstance,
93 + number
94 + > = new WeakMap();
95
96 let getInternalIDForNative: GetFiberIDForNative = ((null: any): GetFiberIDForNative);
97 let findNativeNodeForInternalID: (id: number) => ?NativeType;
@@ -143,9 +147,15 @@ export function attach(
147
148 let oldReconcilerMethods = null;
149 if (renderer.Reconciler) {
150 + // React 15
151 oldReconcilerMethods = decorateMany(renderer.Reconciler, {
152 mountComponent(fn, args) {
148 - const [internalInstance] = args;
153 + const [
154 + internalInstance,
155 + transaction,
156 + hostParent,
157 + hostContainerInfo,
158 + ] = args;
159 if (getElementType(internalInstance) === ElementTypeOtherOrUnknown) {
160 return fn.apply(this, args);
161 }
@@ -160,6 +170,12 @@ export function attach(
170 recordMount(internalInstance, id, parentID);
171 parentIDStack.push(id);
172
173 + // Remember the root.
174 + internalInstanceToRootIDMap.set(
175 + internalInstance,
176 + getID(hostContainerInfo._topLevelWrapper)
177 + );
178 +
179 try {
180 const result = fn.apply(this, args);
181 parentIDStack.pop();
@@ -169,7 +185,11 @@ export function attach(
185 throw err;
186 } finally {
187 if (parentIDStack.length === 0) {
172 - flushPendingEvents(id);
188 + const rootID = internalInstanceToRootIDMap.get(internalInstance);
189 + if (rootID === undefined) {
190 + throw new Error('Expected to find root ID.');
191 + }
192 + flushPendingEvents(rootID);
193 }
194 }
195 },
@@ -199,11 +219,10 @@ export function attach(
219 throw err;
220 } finally {
221 if (parentIDStack.length === 0) {
202 - // TODO: this is probably wrong!
203 - const rootID =
204 - internalInstance._topLevelWrapper === null
205 - ? id
206 - : getID(internalInstance._topLevelWrapper);
222 + const rootID = internalInstanceToRootIDMap.get(internalInstance);
223 + if (rootID === undefined) {
224 + throw new Error('Expected to find root ID.');
225 + }
226 flushPendingEvents(rootID);
227 }
228 }
@@ -234,11 +253,10 @@ export function attach(
253 throw err;
254 } finally {
255 if (parentIDStack.length === 0) {
237 - // TODO: this is probably wrong!
238 - const rootID =
239 - internalInstance._topLevelWrapper === null
240 - ? id
241 - : getID(internalInstance._topLevelWrapper);
256 + const rootID = internalInstanceToRootIDMap.get(internalInstance);
257 + if (rootID === undefined) {
258 + throw new Error('Expected to find root ID.');
259 + }
260 flushPendingEvents(rootID);
261 }
262 }
@@ -264,7 +282,11 @@ export function attach(
282 throw err;
283 } finally {
284 if (parentIDStack.length === 0) {
267 - flushPendingEvents(id);
285 + const rootID = internalInstanceToRootIDMap.get(internalInstance);
286 + if (rootID === undefined) {
287 + throw new Error('Expected to find root ID.');
288 + }
289 + flushPendingEvents(rootID);
290 }
291 }
292 },
@@ -346,7 +368,6 @@ export function attach(
368
369 function recordUnmount(internalInstance: InternalInstance, id: number) {
370 pendingUnmountedIDs.push(id);
349 - internalInstanceToIDMap.delete(internalInstance);
371 idToInternalInstanceMap.delete(id);
372 }
373
@@ -525,9 +546,11 @@ export function attach(
546 if (owner) {
547 owners = [];
548 while (owner != null) {
549 + const ownerData = getData(owner);
550 owners.push({
529 - displayName: getData(owner).displayName || 'Unknown',
551 + displayName: ownerData.displayName || 'Unknown',
552 id: getID(owner),
553 + type: ownerData.type,
554 });
555 owner = owner.owner;
556 }
@@ -555,6 +578,8 @@ export function attach(
578
579 displayName: data.displayName,
580
581 + type: data.type,
582 +
583 // New events system did not exist in legacy versions
584 events: null,
585
@@ -717,23 +742,26 @@ export function attach(
742 };
743
744 function getBestMatchForTrackedPath(): PathMatch | null {
720 - return null; // TODO (legacy)
745 + // Not implemented.
746 + return null;
747 }
748
749 function getPathForElement(id: number): Array<PathFrame> | null {
724 - return null; // TODO (legacy)
750 + // Not implemented.
751 + return null;
752 }
753
754 function updateComponentFilters(componentFilters: Array<ComponentFilter>) {
728 - // TODO (legacy)
755 + // Not implemented.
756 }
757
758 function setTrackedPath(path: Array<PathFrame> | null) {
732 - // TODO (legacy)
759 + // Not implemented.
760 }
761
762 function getOwnersList(id: number): Array<Owner> | null {
736 - return null; // TODO (legacy)
763 + // Not implemented.
764 + return null;
765 }
766
767 return {