@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,60 @@
1
+ import { SyncHook } from "tapable-ts";
2
+ import { describe, expect, test } from "vitest";
3
+ import { addProfilerInterceptorsToHooks } from "../addProfilerInterceptorsToHooks";
4
+ import { Profiler } from "../helpers";
5
+
6
+ describe("addProfilerInterceptorsToHooks", () => {
7
+ /**
8
+ * When a parent hook's call interceptor fires, it recursively calls
9
+ * addProfilerInterceptorsToHooks on args[0] to discover nested hooks.
10
+ * If the parent hook fires again with the same child object, the intercepted
11
+ * WeakSet prevents a second interceptor from being added — so the child hook
12
+ * always fires exactly once per call regardless of how many times the parent
13
+ * hook has fired.
14
+ */
15
+ test("re-intercepting the same child object on repeated parent calls does not duplicate timers", () => {
16
+ const profilerInstance = new Profiler();
17
+ profilerInstance.start();
18
+
19
+ // Child object whose hooks get discovered lazily via the parent's call arg
20
+ const childObj = {
21
+ hooks: {
22
+ afterTransition: new SyncHook<[]>(),
23
+ },
24
+ };
25
+
26
+ // Parent hook that passes childObj as its argument (mirrors Player's "flow" hook
27
+ // passing a FlowInstance, which carries its own hooks like afterTransition)
28
+ const parentObj = {
29
+ hooks: {
30
+ flow: new SyncHook<[typeof childObj]>(),
31
+ },
32
+ };
33
+
34
+ addProfilerInterceptorsToHooks(parentObj, profilerInstance);
35
+
36
+ // First parent call: wires one interceptor onto childObj.hooks.afterTransition
37
+ parentObj.hooks.flow.call(childObj);
38
+ childObj.hooks.afterTransition.call();
39
+
40
+ const snapAfterFirst = profilerInstance.getSnapshot();
41
+ expect(snapAfterFirst.rootNodes).toHaveLength(2);
42
+ expect(snapAfterFirst.rootNodes[0]!.name).toBe("flow");
43
+ expect(snapAfterFirst.rootNodes[1]!.name).toBe("afterTransition");
44
+ expect(snapAfterFirst.rootNodes[1]!.children).toHaveLength(0);
45
+
46
+ profilerInstance.start();
47
+
48
+ // Second parent call with the same childObj: the WeakSet guard prevents a second
49
+ // interceptor from being added to afterTransition — it fires exactly once.
50
+ parentObj.hooks.flow.call(childObj);
51
+ childObj.hooks.afterTransition.call();
52
+
53
+ const snapAfterSecond = profilerInstance.getSnapshot();
54
+ const afterTransitionNode = snapAfterSecond.rootNodes.find(
55
+ (n) => n.name === "afterTransition",
56
+ );
57
+ expect(afterTransitionNode).toBeDefined();
58
+ expect(afterTransitionNode!.children).toHaveLength(0);
59
+ });
60
+ });
@@ -0,0 +1,168 @@
1
+ import { Flow, InProgressState, Player } from "@player-ui/player";
2
+ import { describe, expect, test, vi } from "vitest";
3
+ import { ProfilerDevtoolsPlugin } from "../plugin";
4
+
5
+ let count = 2490.0;
6
+ vi.mock("@player-devtools/plugin", async () => {
7
+ const actual = await vi.importActual("@player-devtools/plugin");
8
+ return {
9
+ ...actual,
10
+ getNowTime: vi.fn(() => {
11
+ count += 0.1;
12
+ return count;
13
+ }),
14
+ };
15
+ });
16
+
17
+ describe("Plugin", () => {
18
+ // This test is being used to setup a baseline snapshot of perf on a basic player flow.
19
+ test("should profile player hooks when navigating through a flow", async () => {
20
+ const profilerPlugin = new ProfilerDevtoolsPlugin({
21
+ handler: {
22
+ checkIfDevtoolsIsActive: () => true,
23
+ processInteraction: () => {},
24
+ },
25
+ playerID: "ID",
26
+ });
27
+
28
+ const player = new Player({ plugins: [profilerPlugin] });
29
+
30
+ // This flow is used to navigate through common player steps.
31
+ const flow: Flow = {
32
+ id: "flow",
33
+ views: [
34
+ {
35
+ id: "view1",
36
+ type: "foo",
37
+ value: "bar",
38
+ },
39
+ ],
40
+ navigation: {
41
+ BEGIN: "FLOW_1",
42
+ FLOW_1: {
43
+ startState: "VIEW_1",
44
+ VIEW_1: {
45
+ state_type: "VIEW",
46
+ ref: "view1",
47
+ transitions: {
48
+ "*": "ACTION_1",
49
+ },
50
+ },
51
+ ACTION_1: {
52
+ state_type: "ACTION",
53
+ exp: "{{a}} = 1",
54
+ transitions: {
55
+ "*": "END_DONE",
56
+ },
57
+ },
58
+ END_DONE: {
59
+ state_type: "END",
60
+ outcome: "done",
61
+ },
62
+ },
63
+ },
64
+ };
65
+ const playerPromise = player.start(flow);
66
+
67
+ // Wait for first view update to complete.
68
+ await vi.waitFor(() => {
69
+ const playerState = player.getState();
70
+ expect(playerState.status).toBe("in-progress");
71
+ expect(
72
+ (playerState as InProgressState).controllers.view.currentView
73
+ ?.lastUpdate,
74
+ ).toBeDefined();
75
+ });
76
+
77
+ // Live update: profiling is active
78
+ const liveData =
79
+ profilerPlugin.store.getState().plugins["player-ui-profiler-plugin"]?.flow
80
+ .data;
81
+ expect(liveData?.profiling).toBe(true);
82
+
83
+ // Transition to action state
84
+ (player.getState() as InProgressState).controllers.flow.transition("go");
85
+
86
+ // Wait for action state to transition to end state and complete
87
+ await playerPromise;
88
+ profilerPlugin.processInteraction({
89
+ payload: {
90
+ type: "stop-profiling",
91
+ },
92
+ type: "PLAYER_DEVTOOLS_PLUGIN_INTERACTION",
93
+ });
94
+
95
+ const storeState = profilerPlugin.store.getState();
96
+ expect(
97
+ storeState.plugins["player-ui-profiler-plugin"]?.flow.data,
98
+ ).toMatchSnapshot();
99
+ });
100
+
101
+ test("stop-profiling marks profiling complete; start-profiling resets state", async () => {
102
+ const profilerPlugin = new ProfilerDevtoolsPlugin({
103
+ handler: {
104
+ checkIfDevtoolsIsActive: () => true,
105
+ processInteraction: () => {},
106
+ },
107
+ playerID: "ID",
108
+ });
109
+
110
+ const player = new Player({ plugins: [profilerPlugin] });
111
+
112
+ const flow: Flow = {
113
+ id: "flow2",
114
+ views: [{ id: "view1", type: "foo" }],
115
+ navigation: {
116
+ BEGIN: "FLOW_1",
117
+ FLOW_1: {
118
+ startState: "VIEW_1",
119
+ VIEW_1: {
120
+ state_type: "VIEW",
121
+ ref: "view1",
122
+ transitions: { "*": "END_DONE" },
123
+ },
124
+ END_DONE: { state_type: "END", outcome: "done" },
125
+ },
126
+ },
127
+ };
128
+
129
+ const playerPromise = player.start(flow);
130
+
131
+ // Wait for the view to render so hooks have fired
132
+ await vi.waitFor(() => {
133
+ const playerState = player.getState();
134
+ expect(playerState.status).toBe("in-progress");
135
+ expect(
136
+ (playerState as InProgressState).controllers.view.currentView
137
+ ?.lastUpdate,
138
+ ).toBeDefined();
139
+ });
140
+
141
+ profilerPlugin.processInteraction({
142
+ payload: { type: "stop-profiling" },
143
+ type: "PLAYER_DEVTOOLS_PLUGIN_INTERACTION",
144
+ });
145
+
146
+ const dataAfterStop =
147
+ profilerPlugin.store.getState().plugins["player-ui-profiler-plugin"]?.flow
148
+ .data;
149
+ expect(dataAfterStop?.profiling).toBe(false);
150
+ expect(dataAfterStop?.displayFlameGraph).toBe(true);
151
+
152
+ // Restart — state should flip back to active profiling
153
+ profilerPlugin.processInteraction({
154
+ payload: { type: "start-profiling" },
155
+ type: "PLAYER_DEVTOOLS_PLUGIN_INTERACTION",
156
+ });
157
+
158
+ const dataAfterRestart =
159
+ profilerPlugin.store.getState().plugins["player-ui-profiler-plugin"]?.flow
160
+ .data;
161
+ expect(dataAfterRestart?.profiling).toBe(true);
162
+ expect(dataAfterRestart?.displayFlameGraph).toBe(false);
163
+
164
+ // Clean up
165
+ (player.getState() as InProgressState).controllers.flow.transition("go");
166
+ await playerPromise;
167
+ });
168
+ });
@@ -0,0 +1,89 @@
1
+ import {
2
+ AsyncParallelBailHook,
3
+ AsyncParallelHook,
4
+ AsyncSeriesBailHook,
5
+ AsyncSeriesHook,
6
+ AsyncSeriesLoopHook,
7
+ AsyncSeriesWaterfallHook,
8
+ SyncBailHook,
9
+ SyncHook,
10
+ SyncLoopHook,
11
+ SyncWaterfallHook,
12
+ } from "tapable-ts";
13
+ import { Profiler, hasHooks, isMatchingPaths, isRecordType } from "./helpers";
14
+
15
+ /* Paths to hooks to ignore.
16
+ * Currently ignoring "view" hook on player since it acts as a shortcut to the viewController's view hook. Including it would duplicate a lot of profiling work.
17
+ */
18
+ const IGNORED_PATHS = [["view"]];
19
+
20
+ // Would love to just check if things are `Hook` but tapable-ts doesn't export the base class ;-;
21
+ type AnyHook =
22
+ | AsyncParallelBailHook<unknown[], unknown>
23
+ | AsyncParallelHook<unknown[]>
24
+ | AsyncSeriesBailHook<unknown[], unknown>
25
+ | AsyncSeriesHook<unknown[]>
26
+ | AsyncSeriesLoopHook<unknown[]>
27
+ | AsyncSeriesWaterfallHook<unknown[]>
28
+ | SyncBailHook<unknown[], unknown>
29
+ | SyncHook<unknown[]>
30
+ | SyncLoopHook<unknown[]>
31
+ | SyncWaterfallHook<unknown[]>;
32
+
33
+ // Note: cannot use instanceof to check against the hook classes due to how JS is loaded in swift and kotlin
34
+ const isAnyHook = (obj: unknown): obj is AnyHook => {
35
+ return (
36
+ isRecordType(obj) &&
37
+ "intercept" in obj &&
38
+ typeof obj.intercept === "function"
39
+ );
40
+ };
41
+
42
+ /** Recursively add profiler interceptors to each hook in the "hooks" property of obj. */
43
+ export const addProfilerInterceptorsToHooks = (
44
+ obj: unknown,
45
+ profiler: Profiler,
46
+ currentPath: string[] = [],
47
+ intercepted: WeakSet<object> = new WeakSet(),
48
+ ): void => {
49
+ if (!hasHooks(obj)) {
50
+ return;
51
+ }
52
+
53
+ Object.entries(obj.hooks).forEach(([key, value]) => {
54
+ const nextPath = [...currentPath, key];
55
+ if (
56
+ !isAnyHook(value) ||
57
+ IGNORED_PATHS.some((path) => isMatchingPaths(path, nextPath)) ||
58
+ intercepted.has(value)
59
+ ) {
60
+ return;
61
+ }
62
+
63
+ intercepted.add(value);
64
+
65
+ value.intercept({
66
+ call: (...args) => {
67
+ if (args.length > 0) {
68
+ addProfilerInterceptorsToHooks(
69
+ args[0],
70
+ profiler,
71
+ nextPath,
72
+ intercepted,
73
+ );
74
+ }
75
+
76
+ profiler.startTimer(key);
77
+ },
78
+ done: () => {
79
+ profiler.endTimer({ hookName: key });
80
+ },
81
+ result: () => {
82
+ profiler.endTimer({ hookName: key });
83
+ },
84
+ error: () => {
85
+ profiler.endTimer({ hookName: key });
86
+ },
87
+ });
88
+ });
89
+ };
@@ -0,0 +1,7 @@
1
+ // Source of truth for these lives in the content package so the DSL-authored
2
+ // flow and the runtime plugin can't drift.
3
+ export {
4
+ PLUGIN_ID,
5
+ VIEWS_IDS,
6
+ INTERACTIONS,
7
+ } from "@player-devtools/profiler-plugin-content";
@@ -0,0 +1,55 @@
1
+ import { beforeEach, describe, expect, test, vi } from "vitest";
2
+ import { isRecordType } from "../isRecordType";
3
+ import { hasHooks } from "../hasHooks";
4
+
5
+ vi.mock("../isRecordType", () => ({
6
+ isRecordType: vi.fn(),
7
+ }));
8
+
9
+ const mockIsRecordType = vi.mocked(isRecordType);
10
+
11
+ beforeEach(() => {
12
+ vi.clearAllMocks();
13
+ });
14
+
15
+ describe("hasHooks", () => {
16
+ test("returns true when both obj and obj.hooks are records", () => {
17
+ const hooks = { someHook: () => {} };
18
+ const input = { hooks };
19
+ // First call (on obj) and second call (on obj.hooks) both return true
20
+ mockIsRecordType.mockImplementation(
21
+ (arg) => arg === input || arg === hooks,
22
+ );
23
+
24
+ expect(hasHooks(input)).toBe(true);
25
+ expect(mockIsRecordType).toHaveBeenNthCalledWith(1, input);
26
+ expect(mockIsRecordType).toHaveBeenNthCalledWith(2, hooks);
27
+ });
28
+
29
+ test("short-circuits to false when obj is not a record", () => {
30
+ mockIsRecordType.mockReturnValue(false);
31
+
32
+ expect(hasHooks("not a record")).toBe(false);
33
+ // Bails after the first guard — never checks obj.hooks
34
+ expect(mockIsRecordType).toHaveBeenCalledTimes(1);
35
+ });
36
+
37
+ test("returns false when the hooks key is absent", () => {
38
+ // obj is a record, but it has no "hooks" key, so the "in" check fails
39
+ mockIsRecordType.mockReturnValue(true);
40
+
41
+ expect(hasHooks({ notHooks: {} })).toBe(false);
42
+ // Only obj is checked; the "hooks" in obj check short-circuits before the second call
43
+ expect(mockIsRecordType).toHaveBeenCalledTimes(1);
44
+ });
45
+
46
+ test("returns false when obj.hooks is not a record", () => {
47
+ const input = { hooks: [] };
48
+ // obj is a record, but obj.hooks is not
49
+ mockIsRecordType.mockImplementation((arg) => arg === input);
50
+
51
+ expect(hasHooks(input)).toBe(false);
52
+ expect(mockIsRecordType).toHaveBeenCalledTimes(2);
53
+ expect(mockIsRecordType).toHaveBeenNthCalledWith(2, input.hooks);
54
+ });
55
+ });
@@ -0,0 +1,24 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { isMatchingPaths } from "../isMatchingPaths";
3
+
4
+ describe("isMatchingPaths", () => {
5
+ test("returns true for identical paths", () => {
6
+ expect(isMatchingPaths(["a", "b", "c"], ["a", "b", "c"])).toBe(true);
7
+ });
8
+
9
+ test("returns true for two empty paths", () => {
10
+ expect(isMatchingPaths([], [])).toBe(true);
11
+ });
12
+
13
+ test("returns false when lengths differ", () => {
14
+ expect(isMatchingPaths(["a"], ["a", "b"])).toBe(false);
15
+ });
16
+
17
+ test("returns false when an element differs", () => {
18
+ expect(isMatchingPaths(["a", "b"], ["a", "c"])).toBe(false);
19
+ });
20
+
21
+ test("is order sensitive", () => {
22
+ expect(isMatchingPaths(["a", "b"], ["b", "a"])).toBe(false);
23
+ });
24
+ });
@@ -0,0 +1,31 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { isRecordType } from "../isRecordType";
3
+
4
+ describe("isRecordType", () => {
5
+ test("returns true for a plain object", () => {
6
+ expect(isRecordType({ a: 1 })).toBe(true);
7
+ });
8
+
9
+ test("returns true for an empty object", () => {
10
+ expect(isRecordType({})).toBe(true);
11
+ });
12
+
13
+ test("returns false for an array", () => {
14
+ expect(isRecordType([1, 2, 3])).toBe(false);
15
+ });
16
+
17
+ test("returns false for null", () => {
18
+ expect(isRecordType(null)).toBe(false);
19
+ });
20
+
21
+ test("returns false for primitives", () => {
22
+ expect(isRecordType("string")).toBe(false);
23
+ expect(isRecordType(42)).toBe(false);
24
+ expect(isRecordType(true)).toBe(false);
25
+ expect(isRecordType(undefined)).toBe(false);
26
+ });
27
+
28
+ test("returns false for a function", () => {
29
+ expect(isRecordType(() => {})).toBe(false);
30
+ });
31
+ });