@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
@@ -0,0 +1,325 @@
1
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/plugin.ts
2
+ import {
3
+ DevtoolsPlugin,
4
+ genDataChangeTransaction
5
+ } from "@player-devtools/plugin";
6
+ import {
7
+ INTERACTIONS,
8
+ ProfilerPluginData
9
+ } from "@player-devtools/profiler-plugin-content";
10
+ import { dset } from "dset/merge";
11
+ import { produce } from "immer";
12
+
13
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isRecordType.ts
14
+ var isRecordType = (obj) => typeof obj === "object" && obj !== null && !Array.isArray(obj);
15
+
16
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/hasHooks.ts
17
+ var hasHooks = (obj) => {
18
+ return isRecordType(obj) && "hooks" in obj && isRecordType(obj.hooks);
19
+ };
20
+
21
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isMatchingPaths.ts
22
+ var isMatchingPaths = (path1, path2) => {
23
+ if (path1.length !== path2.length) return false;
24
+ return path1.every((val, idx) => val === path2[idx]);
25
+ };
26
+
27
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/profiler.ts
28
+ import { getNowTime } from "@player-devtools/plugin";
29
+ var Profiler = class {
30
+ constructor(onUpdate) {
31
+ this.onUpdate = onUpdate;
32
+ this.rootNodes = [];
33
+ this.stack = [];
34
+ }
35
+ start() {
36
+ this.rootNodes = [];
37
+ this.stack = [];
38
+ this.onUpdate?.();
39
+ }
40
+ clear() {
41
+ const now = getNowTime();
42
+ for (const node of this.stack) {
43
+ node.startTime = now;
44
+ node.children = node.children.filter((x) => x.endTime === void 0);
45
+ }
46
+ for (let i = 1; i < this.stack.length; i++) {
47
+ this.stack[i - 1].children = [this.stack[i]];
48
+ }
49
+ this.rootNodes = this.stack.slice(0, 1);
50
+ this.onUpdate?.();
51
+ }
52
+ cloneNode(node, snapshotTime) {
53
+ const children = node.children.map((c) => this.cloneNode(c, snapshotTime));
54
+ const endTime = node.endTime ?? (snapshotTime !== void 0 ? snapshotTime : void 0);
55
+ const value = node.value ?? (endTime !== void 0 && node.startTime !== void 0 ? Math.ceil((endTime - node.startTime) * 1e3) : children.reduce((prev, current) => prev + (current?.value ?? 0), 0));
56
+ return {
57
+ ...node,
58
+ endTime,
59
+ value,
60
+ children
61
+ };
62
+ }
63
+ getSnapshot() {
64
+ const now = getNowTime();
65
+ return {
66
+ rootNodes: this.rootNodes.map((n) => this.cloneNode(n, now))
67
+ };
68
+ }
69
+ startTimer(hookName) {
70
+ const node = {
71
+ name: hookName,
72
+ startTime: getNowTime(),
73
+ children: []
74
+ };
75
+ if (this.stack.length > 0) {
76
+ this.stack[this.stack.length - 1].children.push(node);
77
+ } else {
78
+ this.rootNodes.push(node);
79
+ }
80
+ this.stack.push(node);
81
+ }
82
+ finalizeNode(node, endTime) {
83
+ const duration = node.startTime !== void 0 ? endTime - node.startTime : 0.01;
84
+ node.endTime = endTime;
85
+ node.value = Math.ceil(duration * 1e3);
86
+ node.tooltip = `${node.name}, ${duration.toFixed(4)} (ms)`;
87
+ }
88
+ endTimer({ hookName }) {
89
+ const idx = [...this.stack].reverse().findIndex((n) => n.name === hookName);
90
+ if (idx === -1) {
91
+ console.warn(`endTimer: '${hookName}' not found in stack, ignoring`);
92
+ return;
93
+ }
94
+ const targetIdx = this.stack.length - 1 - idx;
95
+ const endTime = getNowTime();
96
+ for (let i = this.stack.length - 1; i > targetIdx; i--) {
97
+ const orphan = this.stack[i];
98
+ console.warn(
99
+ `endTimer: popping '${orphan.name}' \u2014 timer was never explicitly ended`
100
+ );
101
+ this.finalizeNode(orphan, endTime);
102
+ }
103
+ this.finalizeNode(this.stack[targetIdx], endTime);
104
+ this.stack.length = targetIdx;
105
+ this.onUpdate?.();
106
+ }
107
+ stopProfiler() {
108
+ return { rootNodes: [...this.rootNodes] };
109
+ }
110
+ };
111
+
112
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/transformProfilerData.ts
113
+ var createSpacer = (gap) => ({
114
+ name: "(work)",
115
+ value: Math.ceil(gap * 1e3),
116
+ children: [],
117
+ backgroundColor: "#000000",
118
+ color: "#000000",
119
+ tooltip: "Placeholder time between hooks"
120
+ });
121
+ var transformProfilerData = (root) => {
122
+ return {
123
+ ...root,
124
+ children: transformProfilerDataHelper(root.children, root.startTime)
125
+ };
126
+ };
127
+ var transformProfilerDataHelper = (nodes, parentStart = 0) => {
128
+ const merged = [];
129
+ let cursor = parentStart;
130
+ for (const node of nodes) {
131
+ if (node.startTime === void 0 || node.endTime === void 0 || node.value === void 0 || node.value <= 0) {
132
+ continue;
133
+ }
134
+ const next = {
135
+ ...node,
136
+ children: transformProfilerDataHelper(node.children, node.startTime)
137
+ };
138
+ const gap = node.startTime - cursor;
139
+ if (gap > 0) {
140
+ merged.push(createSpacer(gap));
141
+ }
142
+ cursor = node.endTime;
143
+ merged.push(next);
144
+ }
145
+ return merged;
146
+ };
147
+
148
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/addProfilerInterceptorsToHooks.ts
149
+ var IGNORED_PATHS = [["view"]];
150
+ var isAnyHook = (obj) => {
151
+ return isRecordType(obj) && "intercept" in obj && typeof obj.intercept === "function";
152
+ };
153
+ var addProfilerInterceptorsToHooks = (obj, profiler, currentPath = [], intercepted = /* @__PURE__ */ new WeakSet()) => {
154
+ if (!hasHooks(obj)) {
155
+ return;
156
+ }
157
+ Object.entries(obj.hooks).forEach(([key, value]) => {
158
+ const nextPath = [...currentPath, key];
159
+ if (!isAnyHook(value) || IGNORED_PATHS.some((path) => isMatchingPaths(path, nextPath)) || intercepted.has(value)) {
160
+ return;
161
+ }
162
+ intercepted.add(value);
163
+ value.intercept({
164
+ call: (...args) => {
165
+ if (args.length > 0) {
166
+ addProfilerInterceptorsToHooks(
167
+ args[0],
168
+ profiler,
169
+ nextPath,
170
+ intercepted
171
+ );
172
+ }
173
+ profiler.startTimer(key);
174
+ },
175
+ done: () => {
176
+ profiler.endTimer({ hookName: key });
177
+ },
178
+ result: () => {
179
+ profiler.endTimer({ hookName: key });
180
+ },
181
+ error: () => {
182
+ profiler.endTimer({ hookName: key });
183
+ }
184
+ });
185
+ });
186
+ };
187
+
188
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/plugin.ts
189
+ var wrapInRoot = (nodes) => {
190
+ const startTime = nodes.reduce(
191
+ (min, n) => n.startTime !== void 0 && (min === void 0 || n.startTime < min) ? n.startTime : min,
192
+ void 0
193
+ ) ?? 0;
194
+ const endTime = nodes.reduce(
195
+ (max, n) => n.endTime !== void 0 && (max === void 0 || n.endTime > max) ? n.endTime : max,
196
+ void 0
197
+ ) ?? startTime;
198
+ return {
199
+ name: "root",
200
+ startTime,
201
+ endTime,
202
+ value: Math.ceil((endTime - startTime) * 1e3),
203
+ children: nodes
204
+ };
205
+ };
206
+ var pluginData = ProfilerPluginData;
207
+ var pluginID = pluginData.id;
208
+ var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
209
+ constructor(options) {
210
+ super({
211
+ ...options,
212
+ pluginData
213
+ });
214
+ this.name = "ProfilerDevtoolsPlugin";
215
+ this.interactionMap = /* @__PURE__ */ new Map([
216
+ [INTERACTIONS.START_PROFILING, () => this.startProfiler()],
217
+ [INTERACTIONS.STOP_PROFILING, () => this.stopProfiler()],
218
+ [INTERACTIONS.RESET_PROFILING, () => this.clearProfiler()]
219
+ ]);
220
+ this.profilerObj = new Profiler(() => {
221
+ const { rootNodes } = this.profilerObj.getSnapshot();
222
+ const newState = this.produceState(
223
+ [
224
+ ["plugins", pluginID, "flow", "data", "rootNode"],
225
+ transformProfilerData(wrapInRoot(rootNodes))
226
+ ],
227
+ [["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes]
228
+ );
229
+ this.store.dispatch(
230
+ genDataChangeTransaction({
231
+ playerID: this.playerID,
232
+ data: newState.plugins[pluginID]?.flow.data,
233
+ pluginID
234
+ })
235
+ );
236
+ });
237
+ }
238
+ /**
239
+ * Produces a new store state with each `[path, value]` pair written into the
240
+ * draft via `dset`, leaving the live store untouched.
241
+ */
242
+ produceState(...updates) {
243
+ return produce(this.store.getState(), (draft) => {
244
+ updates.forEach(([path, value]) => {
245
+ dset(draft, path, value);
246
+ });
247
+ });
248
+ }
249
+ startProfiler() {
250
+ this.profilerObj.start();
251
+ const newState = produce(this.store.getState(), (draft) => {
252
+ dset(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
253
+ dset(
254
+ draft,
255
+ ["plugins", pluginID, "flow", "data", "displayFlameGraph"],
256
+ false
257
+ );
258
+ });
259
+ this.store.dispatch(
260
+ genDataChangeTransaction({
261
+ playerID: this.playerID,
262
+ data: newState.plugins[pluginID]?.flow.data,
263
+ pluginID
264
+ })
265
+ );
266
+ }
267
+ clearProfiler() {
268
+ this.profilerObj.clear();
269
+ }
270
+ stopProfiler() {
271
+ const result = this.profilerObj.stopProfiler();
272
+ const { rootNodes } = result;
273
+ const newState = this.produceState(
274
+ [
275
+ ["plugins", pluginID, "flow", "data", "rootNode"],
276
+ transformProfilerData(wrapInRoot(rootNodes))
277
+ ],
278
+ [["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes],
279
+ [["plugins", pluginID, "flow", "data", "profiling"], false],
280
+ [["plugins", pluginID, "flow", "data", "displayFlameGraph"], true]
281
+ );
282
+ this.store.dispatch(
283
+ genDataChangeTransaction({
284
+ playerID: this.playerID,
285
+ data: newState.plugins[pluginID]?.flow.data,
286
+ pluginID
287
+ })
288
+ );
289
+ return result;
290
+ }
291
+ apply(player) {
292
+ if (!this.checkIfDevtoolsIsActive()) {
293
+ return;
294
+ }
295
+ super.apply(player);
296
+ addProfilerInterceptorsToHooks(player, this.profilerObj);
297
+ this.profilerObj.start();
298
+ const initialState = produce(this.store.getState(), (draft) => {
299
+ dset(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
300
+ dset(
301
+ draft,
302
+ ["plugins", pluginID, "flow", "data", "displayFlameGraph"],
303
+ false
304
+ );
305
+ });
306
+ this.store.dispatch(
307
+ genDataChangeTransaction({
308
+ playerID: this.playerID,
309
+ data: initialState.plugins[pluginID]?.flow.data,
310
+ pluginID
311
+ })
312
+ );
313
+ }
314
+ processInteraction(interaction) {
315
+ super.processInteraction(interaction);
316
+ const {
317
+ payload: { type }
318
+ } = interaction;
319
+ this.interactionMap.get(type)?.();
320
+ }
321
+ };
322
+ export {
323
+ ProfilerDevtoolsPlugin
324
+ };
325
+ //# sourceMappingURL=index.mjs.map
package/dist/index.mjs ADDED
@@ -0,0 +1,325 @@
1
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/plugin.ts
2
+ import {
3
+ DevtoolsPlugin,
4
+ genDataChangeTransaction
5
+ } from "@player-devtools/plugin";
6
+ import {
7
+ INTERACTIONS,
8
+ ProfilerPluginData
9
+ } from "@player-devtools/profiler-plugin-content";
10
+ import { dset } from "dset/merge";
11
+ import { produce } from "immer";
12
+
13
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isRecordType.ts
14
+ var isRecordType = (obj) => typeof obj === "object" && obj !== null && !Array.isArray(obj);
15
+
16
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/hasHooks.ts
17
+ var hasHooks = (obj) => {
18
+ return isRecordType(obj) && "hooks" in obj && isRecordType(obj.hooks);
19
+ };
20
+
21
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isMatchingPaths.ts
22
+ var isMatchingPaths = (path1, path2) => {
23
+ if (path1.length !== path2.length) return false;
24
+ return path1.every((val, idx) => val === path2[idx]);
25
+ };
26
+
27
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/profiler.ts
28
+ import { getNowTime } from "@player-devtools/plugin";
29
+ var Profiler = class {
30
+ constructor(onUpdate) {
31
+ this.onUpdate = onUpdate;
32
+ this.rootNodes = [];
33
+ this.stack = [];
34
+ }
35
+ start() {
36
+ this.rootNodes = [];
37
+ this.stack = [];
38
+ this.onUpdate?.();
39
+ }
40
+ clear() {
41
+ const now = getNowTime();
42
+ for (const node of this.stack) {
43
+ node.startTime = now;
44
+ node.children = node.children.filter((x) => x.endTime === void 0);
45
+ }
46
+ for (let i = 1; i < this.stack.length; i++) {
47
+ this.stack[i - 1].children = [this.stack[i]];
48
+ }
49
+ this.rootNodes = this.stack.slice(0, 1);
50
+ this.onUpdate?.();
51
+ }
52
+ cloneNode(node, snapshotTime) {
53
+ const children = node.children.map((c) => this.cloneNode(c, snapshotTime));
54
+ const endTime = node.endTime ?? (snapshotTime !== void 0 ? snapshotTime : void 0);
55
+ const value = node.value ?? (endTime !== void 0 && node.startTime !== void 0 ? Math.ceil((endTime - node.startTime) * 1e3) : children.reduce((prev, current) => prev + (current?.value ?? 0), 0));
56
+ return {
57
+ ...node,
58
+ endTime,
59
+ value,
60
+ children
61
+ };
62
+ }
63
+ getSnapshot() {
64
+ const now = getNowTime();
65
+ return {
66
+ rootNodes: this.rootNodes.map((n) => this.cloneNode(n, now))
67
+ };
68
+ }
69
+ startTimer(hookName) {
70
+ const node = {
71
+ name: hookName,
72
+ startTime: getNowTime(),
73
+ children: []
74
+ };
75
+ if (this.stack.length > 0) {
76
+ this.stack[this.stack.length - 1].children.push(node);
77
+ } else {
78
+ this.rootNodes.push(node);
79
+ }
80
+ this.stack.push(node);
81
+ }
82
+ finalizeNode(node, endTime) {
83
+ const duration = node.startTime !== void 0 ? endTime - node.startTime : 0.01;
84
+ node.endTime = endTime;
85
+ node.value = Math.ceil(duration * 1e3);
86
+ node.tooltip = `${node.name}, ${duration.toFixed(4)} (ms)`;
87
+ }
88
+ endTimer({ hookName }) {
89
+ const idx = [...this.stack].reverse().findIndex((n) => n.name === hookName);
90
+ if (idx === -1) {
91
+ console.warn(`endTimer: '${hookName}' not found in stack, ignoring`);
92
+ return;
93
+ }
94
+ const targetIdx = this.stack.length - 1 - idx;
95
+ const endTime = getNowTime();
96
+ for (let i = this.stack.length - 1; i > targetIdx; i--) {
97
+ const orphan = this.stack[i];
98
+ console.warn(
99
+ `endTimer: popping '${orphan.name}' \u2014 timer was never explicitly ended`
100
+ );
101
+ this.finalizeNode(orphan, endTime);
102
+ }
103
+ this.finalizeNode(this.stack[targetIdx], endTime);
104
+ this.stack.length = targetIdx;
105
+ this.onUpdate?.();
106
+ }
107
+ stopProfiler() {
108
+ return { rootNodes: [...this.rootNodes] };
109
+ }
110
+ };
111
+
112
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/transformProfilerData.ts
113
+ var createSpacer = (gap) => ({
114
+ name: "(work)",
115
+ value: Math.ceil(gap * 1e3),
116
+ children: [],
117
+ backgroundColor: "#000000",
118
+ color: "#000000",
119
+ tooltip: "Placeholder time between hooks"
120
+ });
121
+ var transformProfilerData = (root) => {
122
+ return {
123
+ ...root,
124
+ children: transformProfilerDataHelper(root.children, root.startTime)
125
+ };
126
+ };
127
+ var transformProfilerDataHelper = (nodes, parentStart = 0) => {
128
+ const merged = [];
129
+ let cursor = parentStart;
130
+ for (const node of nodes) {
131
+ if (node.startTime === void 0 || node.endTime === void 0 || node.value === void 0 || node.value <= 0) {
132
+ continue;
133
+ }
134
+ const next = {
135
+ ...node,
136
+ children: transformProfilerDataHelper(node.children, node.startTime)
137
+ };
138
+ const gap = node.startTime - cursor;
139
+ if (gap > 0) {
140
+ merged.push(createSpacer(gap));
141
+ }
142
+ cursor = node.endTime;
143
+ merged.push(next);
144
+ }
145
+ return merged;
146
+ };
147
+
148
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/addProfilerInterceptorsToHooks.ts
149
+ var IGNORED_PATHS = [["view"]];
150
+ var isAnyHook = (obj) => {
151
+ return isRecordType(obj) && "intercept" in obj && typeof obj.intercept === "function";
152
+ };
153
+ var addProfilerInterceptorsToHooks = (obj, profiler, currentPath = [], intercepted = /* @__PURE__ */ new WeakSet()) => {
154
+ if (!hasHooks(obj)) {
155
+ return;
156
+ }
157
+ Object.entries(obj.hooks).forEach(([key, value]) => {
158
+ const nextPath = [...currentPath, key];
159
+ if (!isAnyHook(value) || IGNORED_PATHS.some((path) => isMatchingPaths(path, nextPath)) || intercepted.has(value)) {
160
+ return;
161
+ }
162
+ intercepted.add(value);
163
+ value.intercept({
164
+ call: (...args) => {
165
+ if (args.length > 0) {
166
+ addProfilerInterceptorsToHooks(
167
+ args[0],
168
+ profiler,
169
+ nextPath,
170
+ intercepted
171
+ );
172
+ }
173
+ profiler.startTimer(key);
174
+ },
175
+ done: () => {
176
+ profiler.endTimer({ hookName: key });
177
+ },
178
+ result: () => {
179
+ profiler.endTimer({ hookName: key });
180
+ },
181
+ error: () => {
182
+ profiler.endTimer({ hookName: key });
183
+ }
184
+ });
185
+ });
186
+ };
187
+
188
+ // ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/plugin.ts
189
+ var wrapInRoot = (nodes) => {
190
+ const startTime = nodes.reduce(
191
+ (min, n) => n.startTime !== void 0 && (min === void 0 || n.startTime < min) ? n.startTime : min,
192
+ void 0
193
+ ) ?? 0;
194
+ const endTime = nodes.reduce(
195
+ (max, n) => n.endTime !== void 0 && (max === void 0 || n.endTime > max) ? n.endTime : max,
196
+ void 0
197
+ ) ?? startTime;
198
+ return {
199
+ name: "root",
200
+ startTime,
201
+ endTime,
202
+ value: Math.ceil((endTime - startTime) * 1e3),
203
+ children: nodes
204
+ };
205
+ };
206
+ var pluginData = ProfilerPluginData;
207
+ var pluginID = pluginData.id;
208
+ var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
209
+ constructor(options) {
210
+ super({
211
+ ...options,
212
+ pluginData
213
+ });
214
+ this.name = "ProfilerDevtoolsPlugin";
215
+ this.interactionMap = /* @__PURE__ */ new Map([
216
+ [INTERACTIONS.START_PROFILING, () => this.startProfiler()],
217
+ [INTERACTIONS.STOP_PROFILING, () => this.stopProfiler()],
218
+ [INTERACTIONS.RESET_PROFILING, () => this.clearProfiler()]
219
+ ]);
220
+ this.profilerObj = new Profiler(() => {
221
+ const { rootNodes } = this.profilerObj.getSnapshot();
222
+ const newState = this.produceState(
223
+ [
224
+ ["plugins", pluginID, "flow", "data", "rootNode"],
225
+ transformProfilerData(wrapInRoot(rootNodes))
226
+ ],
227
+ [["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes]
228
+ );
229
+ this.store.dispatch(
230
+ genDataChangeTransaction({
231
+ playerID: this.playerID,
232
+ data: newState.plugins[pluginID]?.flow.data,
233
+ pluginID
234
+ })
235
+ );
236
+ });
237
+ }
238
+ /**
239
+ * Produces a new store state with each `[path, value]` pair written into the
240
+ * draft via `dset`, leaving the live store untouched.
241
+ */
242
+ produceState(...updates) {
243
+ return produce(this.store.getState(), (draft) => {
244
+ updates.forEach(([path, value]) => {
245
+ dset(draft, path, value);
246
+ });
247
+ });
248
+ }
249
+ startProfiler() {
250
+ this.profilerObj.start();
251
+ const newState = produce(this.store.getState(), (draft) => {
252
+ dset(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
253
+ dset(
254
+ draft,
255
+ ["plugins", pluginID, "flow", "data", "displayFlameGraph"],
256
+ false
257
+ );
258
+ });
259
+ this.store.dispatch(
260
+ genDataChangeTransaction({
261
+ playerID: this.playerID,
262
+ data: newState.plugins[pluginID]?.flow.data,
263
+ pluginID
264
+ })
265
+ );
266
+ }
267
+ clearProfiler() {
268
+ this.profilerObj.clear();
269
+ }
270
+ stopProfiler() {
271
+ const result = this.profilerObj.stopProfiler();
272
+ const { rootNodes } = result;
273
+ const newState = this.produceState(
274
+ [
275
+ ["plugins", pluginID, "flow", "data", "rootNode"],
276
+ transformProfilerData(wrapInRoot(rootNodes))
277
+ ],
278
+ [["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes],
279
+ [["plugins", pluginID, "flow", "data", "profiling"], false],
280
+ [["plugins", pluginID, "flow", "data", "displayFlameGraph"], true]
281
+ );
282
+ this.store.dispatch(
283
+ genDataChangeTransaction({
284
+ playerID: this.playerID,
285
+ data: newState.plugins[pluginID]?.flow.data,
286
+ pluginID
287
+ })
288
+ );
289
+ return result;
290
+ }
291
+ apply(player) {
292
+ if (!this.checkIfDevtoolsIsActive()) {
293
+ return;
294
+ }
295
+ super.apply(player);
296
+ addProfilerInterceptorsToHooks(player, this.profilerObj);
297
+ this.profilerObj.start();
298
+ const initialState = produce(this.store.getState(), (draft) => {
299
+ dset(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
300
+ dset(
301
+ draft,
302
+ ["plugins", pluginID, "flow", "data", "displayFlameGraph"],
303
+ false
304
+ );
305
+ });
306
+ this.store.dispatch(
307
+ genDataChangeTransaction({
308
+ playerID: this.playerID,
309
+ data: initialState.plugins[pluginID]?.flow.data,
310
+ pluginID
311
+ })
312
+ );
313
+ }
314
+ processInteraction(interaction) {
315
+ super.processInteraction(interaction);
316
+ const {
317
+ payload: { type }
318
+ } = interaction;
319
+ this.interactionMap.get(type)?.();
320
+ }
321
+ };
322
+ export {
323
+ ProfilerDevtoolsPlugin
324
+ };
325
+ //# sourceMappingURL=index.mjs.map