@player-devtools/profiler-plugin 0.13.1--canary.16.915

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/ProfilerDevtoolsPlugin.native.js +2237 -0
  2. package/dist/ProfilerDevtoolsPlugin.native.js.map +1 -0
  3. package/dist/cjs/index.cjs +346 -0
  4. package/dist/cjs/index.cjs.map +1 -0
  5. package/dist/index.legacy-esm.js +325 -0
  6. package/dist/index.mjs +325 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +39 -0
  9. package/src/__tests__/__snapshots__/plugin.test.ts.snap +1484 -0
  10. package/src/__tests__/addProfilerInterceptorsToHooks.test.ts +60 -0
  11. package/src/__tests__/plugin.test.ts +168 -0
  12. package/src/addProfilerInterceptorsToHooks.ts +89 -0
  13. package/src/constants.ts +7 -0
  14. package/src/helpers/__tests__/hasHooks.test.ts +55 -0
  15. package/src/helpers/__tests__/isMatchingPaths.test.ts +24 -0
  16. package/src/helpers/__tests__/isRecordType.test.ts +31 -0
  17. package/src/helpers/__tests__/profiler.test.ts +282 -0
  18. package/src/helpers/__tests__/transformProfilerData.test.ts +151 -0
  19. package/src/helpers/hasHooks.ts +9 -0
  20. package/src/helpers/index.ts +5 -0
  21. package/src/helpers/isMatchingPaths.ts +5 -0
  22. package/src/helpers/isRecordType.ts +2 -0
  23. package/src/helpers/profiler.ts +114 -0
  24. package/src/helpers/transformProfilerData.ts +51 -0
  25. package/src/index.ts +1 -0
  26. package/src/plugin.ts +185 -0
  27. package/src/types.ts +16 -0
  28. package/types/addProfilerInterceptorsToHooks.d.ts +4 -0
  29. package/types/constants.d.ts +2 -0
  30. package/types/helpers/hasHooks.d.ts +5 -0
  31. package/types/helpers/index.d.ts +6 -0
  32. package/types/helpers/isMatchingPaths.d.ts +2 -0
  33. package/types/helpers/isRecordType.d.ts +2 -0
  34. package/types/helpers/profiler.d.ts +22 -0
  35. package/types/helpers/transformProfilerData.d.ts +3 -0
  36. package/types/index.d.ts +2 -0
  37. package/types/plugin.d.ts +20 -0
  38. package/types/types.d.ts +16 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,185 @@
1
+ import {
2
+ DevtoolsPlugin,
3
+ genDataChangeTransaction,
4
+ type DevtoolsPluginOptions,
5
+ } from "@player-devtools/plugin";
6
+ import type {
7
+ DevtoolsPluginInteractionEvent,
8
+ DevtoolsPluginsStore,
9
+ } from "@player-devtools/types";
10
+ import type { Player } from "@player-ui/player";
11
+ import {
12
+ INTERACTIONS,
13
+ ProfilerPluginData,
14
+ } from "@player-devtools/profiler-plugin-content";
15
+ import { dset } from "dset/merge";
16
+ import { produce } from "immer";
17
+ import { Profiler, transformProfilerData } from "./helpers";
18
+ import type { ProfilerNode } from "./types";
19
+ import { addProfilerInterceptorsToHooks } from "./addProfilerInterceptorsToHooks";
20
+
21
+ const wrapInRoot = (nodes: ProfilerNode[]): ProfilerNode => {
22
+ const startTime =
23
+ nodes.reduce<number | undefined>(
24
+ (min, n) =>
25
+ n.startTime !== undefined && (min === undefined || n.startTime < min)
26
+ ? n.startTime
27
+ : min,
28
+ undefined,
29
+ ) ?? 0;
30
+ const endTime =
31
+ nodes.reduce<number | undefined>(
32
+ (max, n) =>
33
+ n.endTime !== undefined && (max === undefined || n.endTime > max)
34
+ ? n.endTime
35
+ : max,
36
+ undefined,
37
+ ) ?? startTime;
38
+ return {
39
+ name: "root",
40
+ startTime,
41
+ endTime,
42
+ value: Math.ceil((endTime - startTime) * 1000),
43
+ children: nodes,
44
+ };
45
+ };
46
+
47
+ const pluginData = ProfilerPluginData;
48
+
49
+ const pluginID = pluginData.id;
50
+
51
+ export class ProfilerDevtoolsPlugin extends DevtoolsPlugin {
52
+ name = "ProfilerDevtoolsPlugin";
53
+
54
+ private readonly profilerObj: Profiler;
55
+
56
+ private readonly interactionMap: Map<string, () => void> = new Map([
57
+ [INTERACTIONS.START_PROFILING, () => this.startProfiler()],
58
+ [INTERACTIONS.STOP_PROFILING, () => this.stopProfiler()],
59
+ [INTERACTIONS.RESET_PROFILING, () => this.clearProfiler()],
60
+ ]);
61
+
62
+ constructor(options: Omit<DevtoolsPluginOptions, "pluginData">) {
63
+ super({
64
+ ...options,
65
+ pluginData,
66
+ });
67
+
68
+ this.profilerObj = new Profiler(() => {
69
+ const { rootNodes } = this.profilerObj.getSnapshot();
70
+ const newState = this.produceState(
71
+ [
72
+ ["plugins", pluginID, "flow", "data", "rootNode"],
73
+ transformProfilerData(wrapInRoot(rootNodes)),
74
+ ],
75
+ [["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes],
76
+ );
77
+ this.store.dispatch(
78
+ genDataChangeTransaction({
79
+ playerID: this.playerID,
80
+ data: newState.plugins[pluginID]?.flow.data,
81
+ pluginID,
82
+ }),
83
+ );
84
+ });
85
+ }
86
+
87
+ /**
88
+ * Produces a new store state with each `[path, value]` pair written into the
89
+ * draft via `dset`, leaving the live store untouched.
90
+ */
91
+ private produceState(
92
+ ...updates: Array<[path: Array<string>, value: unknown]>
93
+ ): DevtoolsPluginsStore {
94
+ return produce(this.store.getState(), (draft) => {
95
+ updates.forEach(([path, value]) => {
96
+ dset(draft, path, value);
97
+ });
98
+ });
99
+ }
100
+
101
+ private startProfiler(): void {
102
+ this.profilerObj.start();
103
+ const newState = produce(this.store.getState(), (draft) => {
104
+ dset(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
105
+ dset(
106
+ draft,
107
+ ["plugins", pluginID, "flow", "data", "displayFlameGraph"],
108
+ false,
109
+ );
110
+ });
111
+ this.store.dispatch(
112
+ genDataChangeTransaction({
113
+ playerID: this.playerID,
114
+ data: newState.plugins[pluginID]?.flow.data,
115
+ pluginID,
116
+ }),
117
+ );
118
+ }
119
+
120
+ private clearProfiler(): void {
121
+ this.profilerObj.clear();
122
+ }
123
+
124
+ private stopProfiler(): ReturnType<Profiler["stopProfiler"]> {
125
+ const result = this.profilerObj.stopProfiler();
126
+ const { rootNodes } = result;
127
+ const newState = this.produceState(
128
+ [
129
+ ["plugins", pluginID, "flow", "data", "rootNode"],
130
+ transformProfilerData(wrapInRoot(rootNodes)),
131
+ ],
132
+ [["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes],
133
+ [["plugins", pluginID, "flow", "data", "profiling"], false],
134
+ [["plugins", pluginID, "flow", "data", "displayFlameGraph"], true],
135
+ );
136
+ this.store.dispatch(
137
+ genDataChangeTransaction({
138
+ playerID: this.playerID,
139
+ data: newState.plugins[pluginID]?.flow.data,
140
+ pluginID,
141
+ }),
142
+ );
143
+ return result;
144
+ }
145
+
146
+ apply(player: Player): void {
147
+ if (!this.checkIfDevtoolsIsActive()) {
148
+ return;
149
+ }
150
+
151
+ super.apply(player);
152
+
153
+ // Hook once for the lifetime of this Player instance
154
+ addProfilerInterceptorsToHooks(player, this.profilerObj);
155
+
156
+ // Start profiling and dispatch initial state
157
+ this.profilerObj.start();
158
+ const initialState = produce(this.store.getState(), (draft) => {
159
+ dset(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
160
+ dset(
161
+ draft,
162
+ ["plugins", pluginID, "flow", "data", "displayFlameGraph"],
163
+ false,
164
+ );
165
+ });
166
+
167
+ this.store.dispatch(
168
+ genDataChangeTransaction({
169
+ playerID: this.playerID,
170
+ data: initialState.plugins[pluginID]?.flow.data,
171
+ pluginID,
172
+ }),
173
+ );
174
+ }
175
+
176
+ processInteraction(interaction: DevtoolsPluginInteractionEvent): void {
177
+ super.processInteraction(interaction);
178
+
179
+ const {
180
+ payload: { type },
181
+ } = interaction;
182
+
183
+ this.interactionMap.get(type)?.();
184
+ }
185
+ }
package/src/types.ts ADDED
@@ -0,0 +1,16 @@
1
+ export type ProfilerNode = {
2
+ /** hook name */
3
+ name: string;
4
+ /* startTime of the hook */
5
+ startTime?: number;
6
+ /** endTime of the hook */
7
+ endTime?: number;
8
+ /** duration casted to a positive integer (multiplied by 1000) */
9
+ value?: number;
10
+ /** tooltip to be shown on hover */
11
+ tooltip?: string;
12
+ /** subhook profiler nodes */
13
+ children: ProfilerNode[];
14
+ backgroundColor?: string;
15
+ color?: string;
16
+ };
@@ -0,0 +1,4 @@
1
+ import { Profiler } from "./helpers";
2
+ /** Recursively add profiler interceptors to each hook in the "hooks" property of obj. */
3
+ export declare const addProfilerInterceptorsToHooks: (obj: unknown, profiler: Profiler, currentPath?: string[], intercepted?: WeakSet<object>) => void;
4
+ //# sourceMappingURL=addProfilerInterceptorsToHooks.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { PLUGIN_ID, VIEWS_IDS, INTERACTIONS, } from "@player-devtools/profiler-plugin-content";
2
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1,5 @@
1
+ export type ObjectWithHooks = {
2
+ hooks: Record<PropertyKey, unknown>;
3
+ };
4
+ export declare const hasHooks: (obj: unknown) => obj is ObjectWithHooks;
5
+ //# sourceMappingURL=hasHooks.d.ts.map
@@ -0,0 +1,6 @@
1
+ export * from "./hasHooks";
2
+ export * from "./isMatchingPaths";
3
+ export * from "./isRecordType";
4
+ export * from "./profiler";
5
+ export * from "./transformProfilerData";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,2 @@
1
+ export declare const isMatchingPaths: (path1: string[], path2: string[]) => boolean;
2
+ //# sourceMappingURL=isMatchingPaths.d.ts.map
@@ -0,0 +1,2 @@
1
+ export declare const isRecordType: <T>(obj: unknown) => obj is Record<PropertyKey, T>;
2
+ //# sourceMappingURL=isRecordType.d.ts.map
@@ -0,0 +1,22 @@
1
+ import type { ProfilerNode } from "../types";
2
+ export declare class Profiler {
3
+ private readonly onUpdate?;
4
+ private rootNodes;
5
+ private stack;
6
+ constructor(onUpdate?: (() => void) | undefined);
7
+ start(): void;
8
+ clear(): void;
9
+ private cloneNode;
10
+ getSnapshot(): {
11
+ rootNodes: ProfilerNode[];
12
+ };
13
+ startTimer(hookName: string): void;
14
+ private finalizeNode;
15
+ endTimer({ hookName }: {
16
+ hookName: string;
17
+ }): void;
18
+ stopProfiler(): {
19
+ rootNodes: ProfilerNode[];
20
+ };
21
+ }
22
+ //# sourceMappingURL=profiler.d.ts.map
@@ -0,0 +1,3 @@
1
+ import type { ProfilerNode } from "../types";
2
+ export declare const transformProfilerData: (root: ProfilerNode) => ProfilerNode;
3
+ //# sourceMappingURL=transformProfilerData.d.ts.map
@@ -0,0 +1,2 @@
1
+ export * from "./plugin";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,20 @@
1
+ import { DevtoolsPlugin, type DevtoolsPluginOptions } from "@player-devtools/plugin";
2
+ import type { DevtoolsPluginInteractionEvent } from "@player-devtools/types";
3
+ import type { Player } from "@player-ui/player";
4
+ export declare class ProfilerDevtoolsPlugin extends DevtoolsPlugin {
5
+ name: string;
6
+ private readonly profilerObj;
7
+ private readonly interactionMap;
8
+ constructor(options: Omit<DevtoolsPluginOptions, "pluginData">);
9
+ /**
10
+ * Produces a new store state with each `[path, value]` pair written into the
11
+ * draft via `dset`, leaving the live store untouched.
12
+ */
13
+ private produceState;
14
+ private startProfiler;
15
+ private clearProfiler;
16
+ private stopProfiler;
17
+ apply(player: Player): void;
18
+ processInteraction(interaction: DevtoolsPluginInteractionEvent): void;
19
+ }
20
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1,16 @@
1
+ export type ProfilerNode = {
2
+ /** hook name */
3
+ name: string;
4
+ startTime?: number;
5
+ /** endTime of the hook */
6
+ endTime?: number;
7
+ /** duration casted to a positive integer (multiplied by 1000) */
8
+ value?: number;
9
+ /** tooltip to be shown on hover */
10
+ tooltip?: string;
11
+ /** subhook profiler nodes */
12
+ children: ProfilerNode[];
13
+ backgroundColor?: string;
14
+ color?: string;
15
+ };
16
+ //# sourceMappingURL=types.d.ts.map