Distinguish functions and classes
Dan committed
Jun 12, 2019 at 17:23 UTC
fdf753021e4fa14561b2b3363ae19fd89e67fe51
2 files changed
+43
-52
src/backend/legacy/getData.js
deleted
-40
@@ -1,40 +0,0 @@
1
-// @flow
2
-
3
-import {
4
- ElementTypeClass,
5
- ElementTypeHostComponent,
6
- ElementTypeOtherOrUnknown,
7
-} from 'src/types';
8
-import { getDisplayName } from 'src/utils';
9
-
10
-import type { InternalInstance } from './renderer';
11
-import type { FiberData } from '../types';
12
-
13
-export default function getData(internalInstance: InternalInstance): FiberData {
14
- let displayName = null;
15
- let key = null;
16
- let type = ElementTypeOtherOrUnknown;
17
-
18
- // != used deliberately here to catch undefined and null
19
- if (internalInstance._currentElement != null) {
20
- if (internalInstance._currentElement.key) {
21
- key = String(internalInstance._currentElement.key);
22
- }
23
-
24
- const elementType = internalInstance._currentElement.type;
25
- if (typeof elementType === 'string') {
26
- type = ElementTypeHostComponent;
27
- displayName = elementType;
28
- } else if (typeof elementType === 'function') {
29
- // TODO: detect function components.
30
- type = ElementTypeClass;
31
- displayName = getDisplayName(elementType);
32
- }
33
- }
34
-
35
- return {
36
- displayName,
37
- key,
38
- type,
39
- };
40
-}
src/backend/legacy/renderer.js
+43
-12
@@ -9,13 +9,13 @@ import {
9
} from 'src/types';
10
import { getUID, utfEncodeString, printOperationsArray } from '../../utils';
11
import { cleanForBridge, copyWithSet } from '../utils';
12
+import { getDisplayName } from 'src/utils';
13
import {
14
__DEBUG__,
15
TREE_OPERATION_ADD,
16
TREE_OPERATION_REMOVE,
17
TREE_OPERATION_REORDER_CHILDREN,
18
} from '../../constants';
18
-import getData from './getData';
19
import { decorateMany, forceUpdate, restoreMany } from './utils';
20
21
import type {
@@ -32,12 +32,41 @@ import type { Owner, InspectedElement } from '../types';
32
export type InternalInstance = Object;
33
type LegacyRenderer = Object;
34
35
+function getData(internalInstance: InternalInstance) {
36
+ let displayName = null;
37
+ let key = null;
38
+
39
+ // != used deliberately here to catch undefined and null
40
+ if (internalInstance._currentElement != null) {
41
+ if (internalInstance._currentElement.key) {
42
+ key = String(internalInstance._currentElement.key);
43
+ }
44
+
45
+ const elementType = internalInstance._currentElement.type;
46
+ if (typeof elementType === 'string') {
47
+ displayName = elementType;
48
+ } else if (typeof elementType === 'function') {
49
+ displayName = getDisplayName(elementType);
50
+ }
51
+ }
52
+
53
+ return {
54
+ displayName,
55
+ key,
56
+ };
57
+}
58
+
59
function getElementType(internalInstance: InternalInstance): ElementType {
60
// != used deliberately here to catch undefined and null
61
if (internalInstance._currentElement != null) {
62
const elementType = internalInstance._currentElement.type;
63
if (typeof elementType === 'function') {
40
- return ElementTypeClass;
64
+ const publicInstance = internalInstance.getPublicInstance();
65
+ if (publicInstance !== null) {
66
+ return ElementTypeClass;
67
+ } else {
68
+ return ElementTypeFunction;
69
+ }
70
} else if (typeof elementType === 'string') {
71
return ElementTypeHostComponent;
72
}
@@ -331,7 +360,8 @@ export function attach(
360
pushOperation(0); // isProfilingSupported?
361
pushOperation(hasOwnerMetadata ? 1 : 0);
362
} else {
334
- const { displayName, key, type } = getData(internalInstance);
363
+ const type = getElementType(internalInstance);
364
+ const { displayName, key } = getData(internalInstance);
365
366
const ownerID =
367
internalInstance._currentElement != null &&
@@ -532,7 +562,8 @@ export function attach(
562
563
function inspectElementRaw(id: number): InspectedElement | null {
564
const internalInstance = idToInternalInstanceMap.get(id);
535
- const data = getData(internalInstance);
565
+ const displayName = getData(internalInstance).displayName;
566
+ const type = getElementType(internalInstance);
567
568
let context = null;
569
let owners = null;
@@ -550,11 +581,10 @@ export function attach(
581
if (owner) {
582
owners = [];
583
while (owner != null) {
553
- const ownerData = getData(owner);
584
owners.push({
555
- displayName: ownerData.displayName || 'Unknown',
585
+ displayName: getData(owner).displayName || 'Unknown',
586
id: getID(owner),
557
- type: ownerData.type,
587
+ type: getElementType(owner),
588
});
589
owner = owner.owner;
590
}
@@ -581,12 +611,11 @@ export function attach(
611
canToggleSuspense: false,
612
613
// Can view component source location.
584
- canViewSource:
585
- data.type === ElementTypeClass || data.type === ElementTypeFunction,
614
+ canViewSource: type === ElementTypeClass || type === ElementTypeFunction,
615
587
- displayName: data.displayName,
616
+ displayName: displayName,
617
589
- type: data.type,
618
+ type: type,
619
620
// New events system did not exist in legacy versions
621
events: null,
@@ -627,7 +656,7 @@ export function attach(
656
console.log('State:', result.state);
657
}
658
if (result.context !== null) {
630
- console.log('State:', result.context);
659
+ console.log('Context:', result.context);
660
}
661
const nativeNode = findNativeNodeForInternalID(id);
662
if (nativeNode !== null) {
@@ -668,6 +697,8 @@ export function attach(
697
698
switch (getElementType(internalInstance)) {
699
case ElementTypeClass:
700
+ global.$r = internalInstance._instance;
701
+ break;
702
case ElementTypeFunction:
703
const element = internalInstance._currentElement;
704
if (element == null) {