Skip to content

Commit 19836e2

Browse files
authored
chore: add log_effect_tree utility (#15780)
1 parent 3d9a9ab commit 19836e2

File tree

1 file changed

+109
-0
lines changed
  • packages/svelte/src/internal/client/dev

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/** @import { Derived, Effect, Value } from '#client' */
2+
3+
import {
4+
BLOCK_EFFECT,
5+
BOUNDARY_EFFECT,
6+
BRANCH_EFFECT,
7+
CLEAN,
8+
DERIVED,
9+
EFFECT,
10+
MAYBE_DIRTY,
11+
RENDER_EFFECT,
12+
ROOT_EFFECT
13+
} from '../constants.js';
14+
15+
/**
16+
*
17+
* @param {Effect} effect
18+
*/
19+
export function root(effect) {
20+
while (effect.parent !== null) {
21+
effect = effect.parent;
22+
}
23+
24+
return effect;
25+
}
26+
27+
/**
28+
*
29+
* @param {Effect} effect
30+
*/
31+
export function log_effect_tree(effect, depth = 0) {
32+
const flags = effect.f;
33+
34+
let label = '(unknown)';
35+
36+
if ((flags & ROOT_EFFECT) !== 0) {
37+
label = 'root';
38+
} else if ((flags & BOUNDARY_EFFECT) !== 0) {
39+
label = 'boundary';
40+
} else if ((flags & BLOCK_EFFECT) !== 0) {
41+
label = 'block';
42+
} else if ((flags & BRANCH_EFFECT) !== 0) {
43+
label = 'branch';
44+
} else if ((flags & RENDER_EFFECT) !== 0) {
45+
label = 'render effect';
46+
} else if ((flags & EFFECT) !== 0) {
47+
label = 'effect';
48+
}
49+
50+
let status =
51+
(flags & CLEAN) !== 0 ? 'clean' : (flags & MAYBE_DIRTY) !== 0 ? 'maybe dirty' : 'dirty';
52+
53+
// eslint-disable-next-line no-console
54+
console.group(`%c${label} (${status})`, `font-weight: ${status === 'clean' ? 'normal' : 'bold'}`);
55+
56+
if (depth === 0) {
57+
const callsite = new Error().stack
58+
?.split('\n')[2]
59+
.replace(/\s+at (?: \w+\(?)?(.+)\)?/, (m, $1) => $1.replace(/\?[^:]+/, ''));
60+
61+
// eslint-disable-next-line no-console
62+
console.log(callsite);
63+
}
64+
65+
if (effect.deps !== null) {
66+
// eslint-disable-next-line no-console
67+
console.groupCollapsed('%cdeps', 'font-weight: normal');
68+
69+
for (const dep of effect.deps) {
70+
log_dep(dep);
71+
}
72+
73+
// eslint-disable-next-line no-console
74+
console.groupEnd();
75+
}
76+
77+
let child = effect.first;
78+
while (child !== null) {
79+
log_effect_tree(child, depth + 1);
80+
child = child.next;
81+
}
82+
83+
// eslint-disable-next-line no-console
84+
console.groupEnd();
85+
}
86+
87+
/**
88+
*
89+
* @param {Value} dep
90+
*/
91+
function log_dep(dep) {
92+
if ((dep.f & DERIVED) !== 0) {
93+
const derived = /** @type {Derived} */ (dep);
94+
95+
// eslint-disable-next-line no-console
96+
console.groupCollapsed('%cderived', 'font-weight: normal', derived.v);
97+
if (derived.deps) {
98+
for (const d of derived.deps) {
99+
log_dep(d);
100+
}
101+
}
102+
103+
// eslint-disable-next-line no-console
104+
console.groupEnd();
105+
} else {
106+
// eslint-disable-next-line no-console
107+
console.log('state', dep.v);
108+
}
109+
}

0 commit comments

Comments
 (0)