@praxisjs/devtools 0.2.5 → 0.2.7

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.
@@ -1,8 +1,17 @@
1
+ import type { RootComponent } from "@praxisjs/core/internal";
2
+ import {
3
+ createClassDecorator,
4
+ ClassBehavior,
5
+ type ClassEnhancement,
6
+ } from "@praxisjs/decorators";
7
+
1
8
  import { Registry } from "../core/registry";
2
9
 
3
10
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
11
  type AnyConstructor = new (...args: any[]) => any;
5
12
 
13
+ const EXTRA_HOOKS = ["onBeforeUpdate", "onUpdate", "onAfterUpdate"] as const;
14
+
6
15
  /**
7
16
  * Instruments a component class to report renders and lifecycle events
8
17
  * to the devtools panel.
@@ -11,58 +20,52 @@ type AnyConstructor = new (...args: any[]) => any;
11
20
  * @Component()
12
21
  * class MyComponent extends StatefulComponent { ... }
13
22
  */
14
- export function Trace() {
15
- return function <T extends AnyConstructor>(constructor: T, _context: ClassDecoratorContext): T {
16
- const name = constructor.name;
23
+ class TraceBehavior extends ClassBehavior {
24
+ create(instance: RootComponent): ClassEnhancement {
17
25
  const registry = Registry.instance;
18
- const proto = constructor.prototype as Record<string, unknown>;
19
-
20
- // ── render() ──────────────────────────────────────────────────────────
21
- const originalRender = proto.render as
22
- | ((...args: unknown[]) => unknown)
23
- | undefined;
24
26
 
25
- if (originalRender) {
26
- proto.render = function (this: object, ...args: unknown[]) {
27
+ return {
28
+ onMount() {
29
+ registry.recordLifecycle(instance, "onMount");
30
+ },
31
+ onUnmount() {
32
+ registry.recordLifecycle(instance, "onUnmount");
33
+ },
34
+ render(originalRender) {
27
35
  const start = performance.now();
28
- const result = originalRender.call(this, ...args);
36
+ const result = originalRender();
29
37
  const duration = performance.now() - start;
30
- registry.recordRender(this, duration);
38
+ registry.recordRender(instance, duration);
31
39
  return result;
32
- };
33
- }
40
+ },
41
+ };
42
+ }
34
43
 
35
- // ── onBeforeMount – register the component instance ───────────────────
36
- const originalOnBeforeMount = proto.onBeforeMount as
37
- | ((...args: unknown[]) => unknown)
38
- | undefined;
44
+ initialize(Enhanced: AnyConstructor, original: AnyConstructor): void {
45
+ const name = original.name;
46
+ const registry = Registry.instance;
47
+ const proto = Enhanced.prototype as Record<string, unknown>;
39
48
 
49
+ // onBeforeMount — also registers the component instance
50
+ const originalOnBeforeMount = proto.onBeforeMount as ((...args: unknown[]) => unknown) | undefined;
40
51
  proto.onBeforeMount = function (this: object, ...args: unknown[]) {
41
52
  registry.registerComponent(this, name);
42
53
  registry.recordLifecycle(this, "onBeforeMount");
43
54
  return originalOnBeforeMount?.call(this, ...args);
44
55
  };
45
56
 
46
- // ── remaining lifecycle hooks ─────────────────────────────────────────
47
- const hooks = [
48
- "onMount",
49
- "onUnmount",
50
- "onBeforeUpdate",
51
- "onUpdate",
52
- "onAfterUpdate",
53
- ] as const;
54
-
55
- for (const hook of hooks) {
56
- const original = proto[hook] as
57
- | ((...args: unknown[]) => unknown)
58
- | undefined;
59
-
57
+ for (const hook of EXTRA_HOOKS) {
58
+ const orig = proto[hook] as ((...args: unknown[]) => unknown) | undefined;
60
59
  proto[hook] = function (this: object, ...args: unknown[]) {
61
60
  registry.recordLifecycle(this, hook);
62
- return original?.call(this, ...args);
61
+ return orig?.call(this, ...args);
63
62
  };
64
63
  }
64
+ }
65
+ }
66
+
67
+ const traceBehavior = new TraceBehavior();
65
68
 
66
- return constructor;
67
- };
69
+ export function Trace() {
70
+ return createClassDecorator(traceBehavior);
68
71
  }