|
| 1 | +import { createContext, render } from "preact"; |
| 2 | +import { hierarchy, HierarchyNode, partition, PartitionLayout } from "d3-hierarchy"; |
| 3 | +import { |
| 4 | + isModuleTree, |
| 5 | + ModuleLengths, |
| 6 | + ModuleTree, |
| 7 | + ModuleTreeLeaf, |
| 8 | + SizeKey, |
| 9 | + VisualizerData, |
| 10 | +} from "../../shared/types"; |
| 11 | + |
| 12 | +import { generateUniqueId, Id } from "../uid"; |
| 13 | +import { getAvailableSizeOptions } from "../sizes"; |
| 14 | +import { Main } from "./main"; |
| 15 | +import createRainbowColor, { NodeColorGetter } from "./color"; |
| 16 | + |
| 17 | +import "../style/style-flamegraph.scss"; |
| 18 | +import { PADDING } from "./const"; |
| 19 | + |
| 20 | +export interface StaticData { |
| 21 | + data: VisualizerData; |
| 22 | + availableSizeProperties: SizeKey[]; |
| 23 | + width: number; |
| 24 | + height: number; |
| 25 | +} |
| 26 | + |
| 27 | +export interface ModuleIds { |
| 28 | + nodeUid: Id; |
| 29 | + clipUid: Id; |
| 30 | +} |
| 31 | + |
| 32 | +export interface ChartData { |
| 33 | + layout: PartitionLayout<ModuleTree | ModuleTreeLeaf>; |
| 34 | + rawHierarchy: HierarchyNode<ModuleTree | ModuleTreeLeaf>; |
| 35 | + getModuleSize: (node: ModuleTree | ModuleTreeLeaf, sizeKey: SizeKey) => number; |
| 36 | + getModuleIds: (node: ModuleTree | ModuleTreeLeaf) => ModuleIds; |
| 37 | + getModuleColor: NodeColorGetter; |
| 38 | +} |
| 39 | + |
| 40 | +export type Context = StaticData & ChartData; |
| 41 | + |
| 42 | +export const StaticContext = createContext<Context>({} as unknown as Context); |
| 43 | + |
| 44 | +const drawChart = ( |
| 45 | + parentNode: Element, |
| 46 | + data: VisualizerData, |
| 47 | + width: number, |
| 48 | + height: number, |
| 49 | +): void => { |
| 50 | + const availableSizeProperties = getAvailableSizeOptions(data.options); |
| 51 | + |
| 52 | + console.time("layout create"); |
| 53 | + |
| 54 | + const layout = partition<ModuleTree | ModuleTreeLeaf>() |
| 55 | + .size([width, height]) |
| 56 | + .padding(PADDING) |
| 57 | + .round(true); |
| 58 | + |
| 59 | + console.timeEnd("layout create"); |
| 60 | + |
| 61 | + console.time("rawHierarchy create"); |
| 62 | + const rawHierarchy = hierarchy<ModuleTree | ModuleTreeLeaf>(data.tree); |
| 63 | + console.timeEnd("rawHierarchy create"); |
| 64 | + |
| 65 | + const nodeSizesCache = new Map<ModuleTree | ModuleTreeLeaf, ModuleLengths>(); |
| 66 | + |
| 67 | + const nodeIdsCache = new Map<ModuleTree | ModuleTreeLeaf, ModuleIds>(); |
| 68 | + |
| 69 | + const getModuleSize = (node: ModuleTree | ModuleTreeLeaf, sizeKey: SizeKey) => |
| 70 | + nodeSizesCache.get(node)?.[sizeKey] ?? 0; |
| 71 | + |
| 72 | + console.time("rawHierarchy eachAfter cache"); |
| 73 | + rawHierarchy.eachAfter((node) => { |
| 74 | + const nodeData = node.data; |
| 75 | + |
| 76 | + nodeIdsCache.set(nodeData, { |
| 77 | + nodeUid: generateUniqueId("node"), |
| 78 | + clipUid: generateUniqueId("clip"), |
| 79 | + }); |
| 80 | + |
| 81 | + const sizes: ModuleLengths = { renderedLength: 0, gzipLength: 0, brotliLength: 0 }; |
| 82 | + if (isModuleTree(nodeData)) { |
| 83 | + for (const sizeKey of availableSizeProperties) { |
| 84 | + sizes[sizeKey] = nodeData.children.reduce( |
| 85 | + (acc, child) => getModuleSize(child, sizeKey) + acc, |
| 86 | + 0, |
| 87 | + ); |
| 88 | + } |
| 89 | + } else { |
| 90 | + for (const sizeKey of availableSizeProperties) { |
| 91 | + sizes[sizeKey] = data.nodeParts[nodeData.uid][sizeKey] ?? 0; |
| 92 | + } |
| 93 | + } |
| 94 | + nodeSizesCache.set(nodeData, sizes); |
| 95 | + }); |
| 96 | + console.timeEnd("rawHierarchy eachAfter cache"); |
| 97 | + |
| 98 | + const getModuleIds = (node: ModuleTree | ModuleTreeLeaf) => nodeIdsCache.get(node) as ModuleIds; |
| 99 | + |
| 100 | + console.time("color"); |
| 101 | + const getModuleColor = createRainbowColor(rawHierarchy); |
| 102 | + console.timeEnd("color"); |
| 103 | + |
| 104 | + render( |
| 105 | + <StaticContext.Provider |
| 106 | + value={{ |
| 107 | + data, |
| 108 | + availableSizeProperties, |
| 109 | + width, |
| 110 | + height, |
| 111 | + getModuleSize, |
| 112 | + getModuleIds, |
| 113 | + getModuleColor, |
| 114 | + rawHierarchy, |
| 115 | + layout, |
| 116 | + }} |
| 117 | + > |
| 118 | + <Main /> |
| 119 | + </StaticContext.Provider>, |
| 120 | + parentNode, |
| 121 | + ); |
| 122 | +}; |
| 123 | + |
| 124 | +export default drawChart; |
0 commit comments