|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * Utility class to provide the component owner hierarchy to native code for |
| 10 | + * debugging purposes. |
| 11 | + * |
| 12 | + * @providesModule RCTDebugComponentOwnership |
| 13 | + * @flow |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +var DebugComponentOwnershipModule = require('NativeModules').DebugComponentOwnershipModule; |
| 19 | +var InspectorUtils = require('InspectorUtils'); |
| 20 | +var ReactNativeTagHandles = require('ReactNativeTagHandles'); |
| 21 | + |
| 22 | +function componentToString(component) { |
| 23 | + return component.getName ? component.getName() : 'Unknown'; |
| 24 | +} |
| 25 | + |
| 26 | +function getRootTagForTag(tag: number): ?number { |
| 27 | + var rootNodeID = ReactNativeTagHandles.tagToRootNodeID[tag]; |
| 28 | + if (!rootNodeID) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + var rootID = ReactNativeTagHandles.getNativeTopRootIDFromNodeID(rootNodeID); |
| 32 | + if (!rootID) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + return ReactNativeTagHandles.rootNodeIDToTag[rootID]; |
| 36 | +} |
| 37 | + |
| 38 | +module.exports = { |
| 39 | + |
| 40 | + /** |
| 41 | + * Asynchronously returns the owner hierarchy as an array of strings. Request id is |
| 42 | + * passed along to the native module so that the native module can identify the |
| 43 | + * particular call instance. |
| 44 | + * |
| 45 | + * Example returned owner hierarchy: ['RootView', 'Dialog', 'TitleView', 'Text'] |
| 46 | + */ |
| 47 | + getOwnerHierarchy: function(requestID: number, tag: number) { |
| 48 | + var rootTag = getRootTagForTag(tag); |
| 49 | + var instance = InspectorUtils.findInstanceByNativeTag(rootTag, tag); |
| 50 | + var ownerHierarchy = instance ? |
| 51 | + InspectorUtils.getOwnerHierarchy(instance).map(componentToString) : |
| 52 | + null; |
| 53 | + DebugComponentOwnershipModule.receiveOwnershipHierarchy(requestID, tag, ownerHierarchy); |
| 54 | + }, |
| 55 | +}; |
0 commit comments