| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {Facade} from './DevToolsFacade'; |
| 11 | import type { |
| 12 | TreeNode, |
| 13 | NodeInfo, |
| 14 | ComponentSource, |
| 15 | OwnersStack, |
| 16 | ParentEntry, |
| 17 | OwnerEntry, |
| 18 | FindComponentsResult, |
| 19 | ToolError, |
| 20 | } from './DevToolsFacadeTreeTools'; |
| 21 | import type { |
| 22 | StartProfilingResult, |
| 23 | StopProfilingResult, |
| 24 | TraceOverviewRow, |
| 25 | CommitReport, |
| 26 | } from './DevToolsFacadeProfilerTools'; |
| 27 | |
| 28 | import {createTreeTools} from './DevToolsFacadeTreeTools'; |
| 29 | import {createProfilerTools} from './DevToolsFacadeProfilerTools'; |
| 30 | |
| 31 | export type { |
| 32 | TreeNode, |
| 33 | NodeInfo, |
| 34 | HookNode, |
| 35 | ComponentSource, |
| 36 | SourceLocation, |
| 37 | OwnersStack, |
| 38 | ComponentBranchEntry, |
| 39 | ParentEntry, |
| 40 | OwnerEntry, |
| 41 | FindComponentsResult, |
| 42 | ToolError, |
| 43 | } from './DevToolsFacadeTreeTools'; |
| 44 | export type { |
| 45 | CommitComponent, |
| 46 | TraceOverviewRow, |
| 47 | CommitReport, |
| 48 | StartProfilingResult, |
| 49 | StopProfilingResult, |
| 50 | } from './DevToolsFacadeProfilerTools'; |
| 51 | |
| 52 | // The set of tools assembled from a Facade. Each tool returns a plain |
| 53 | // JavaScript value (see the types in ./DevToolsFacadeTreeTools and |
| 54 | // ./DevToolsFacadeProfilerTools); serialization is the integrator's responsibility. |
| 55 | // Integrators decide whether to expose these on globals or call them directly. |
| 56 | export type Tools = { |
| 57 | getComponentTree: ( |
| 58 | depth?: number, |
| 59 | rootUid?: string, |
| 60 | ) => Array<TreeNode> | ToolError, |
| 61 | getComponentByUid: ( |
| 62 | uid: string, |
| 63 | includeHooks?: boolean, |
| 64 | ) => NodeInfo | ToolError, |
| 65 | getComponentByHostInstance: (hostInstance: mixed) => NodeInfo | ToolError, |
| 66 | findComponents: ( |
| 67 | name: string, |
| 68 | rootUid?: string, |
| 69 | page?: number, |
| 70 | pageSize?: number, |
| 71 | ) => FindComponentsResult | ToolError, |
| 72 | getComponentSource: (uid: string) => ComponentSource | ToolError, |
| 73 | getOwnerStackTrace: (uid: string) => OwnersStack | ToolError, |
| 74 | getParentStack: (uid: string) => Array<ParentEntry> | ToolError, |
| 75 | getOwnerStack: (uid: string) => Array<OwnerEntry> | ToolError, |
| 76 | startProfiling: (traceName?: string) => StartProfilingResult | ToolError, |
| 77 | stopProfiling: () => StopProfilingResult | ToolError, |
| 78 | getTraceOverview: (traceName: string) => Array<TraceOverviewRow> | ToolError, |
| 79 | getCommitReport: ( |
| 80 | traceName: string, |
| 81 | commitIndex: number, |
| 82 | ) => CommitReport | ToolError, |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Assemble the set of tools from a Facade. The tools read the facade's tracked |
| 87 | * runtime state (fiber roots, per-renderer internals, profiling state) lazily |
| 88 | * on each call and never touch globals, so the integrator fully owns both the |
| 89 | * facade and the returned tools. Profiler tools share the tree tools' getUid |
| 90 | * so component uids are consistent across all tools. |
| 91 | * |
| 92 | * @param facade - A Facade returned by installFacade(). |
| 93 | */ |
| 94 | export function createTools(facade: Facade): Tools { |
| 95 | const tree = createTreeTools(facade.fiberRoots, facade.rendererInternals); |
| 96 | const profiler = createProfilerTools( |
| 97 | facade.rendererInternals, |
| 98 | facade.profilingState, |
| 99 | tree.getUid, |
| 100 | ); |
| 101 | |
| 102 | return { |
| 103 | getComponentTree: tree.getComponentTree, |
| 104 | getComponentByUid: tree.getComponentByUid, |
| 105 | getComponentByHostInstance: tree.getComponentByHostInstance, |
| 106 | findComponents: tree.findComponents, |
| 107 | getComponentSource: tree.getComponentSource, |
| 108 | getOwnerStackTrace: tree.getOwnerStackTrace, |
| 109 | getParentStack: tree.getParentStack, |
| 110 | getOwnerStack: tree.getOwnerStack, |
| 111 | startProfiling: profiler.startProfiling, |
| 112 | stopProfiling: profiler.stopProfiling, |
| 113 | getTraceOverview: profiler.getTraceOverview, |
| 114 | getCommitReport: profiler.getCommitReport, |
| 115 | }; |
| 116 | } |