Skip to content

Commit d8abb13

Browse files
committed
add modifiers to debug render tree
1 parent dc76897 commit d8abb13

File tree

4 files changed

+187
-4
lines changed

4 files changed

+187
-4
lines changed

packages/@glimmer-workspace/integration-tests/lib/setup-harness.ts

+13
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ export async function setupQunit() {
6363
qunit.moduleDone(pause);
6464
}
6565

66+
// @ts-expect-error missing in types, does exist: https://api.qunitjs.com/callbacks/QUnit.on/#the-testend-event
67+
QUnit.on('testEnd', (testEnd) => {
68+
if (testEnd.status === 'failed') {
69+
testEnd.errors.forEach((assertion: any) => {
70+
console.error(assertion.stack);
71+
// message: speedometer
72+
// actual: 75
73+
// expected: 88
74+
// stack: at dmc.test.js:12
75+
});
76+
}
77+
});
78+
6679
qunit.done(({ failed }) => {
6780
if (failed > 0) {
6881
console.log('[HARNESS] fail');

packages/@glimmer-workspace/integration-tests/test/debug-render-tree-test.ts

+101-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
SimpleNode,
1010
} from '@glimmer/interfaces';
1111
import type { TemplateOnlyComponent } from '@glimmer/runtime';
12-
import { setComponentTemplate } from '@glimmer/manager';
12+
import { modifierCapabilities, setComponentTemplate, setModifierManager } from '@glimmer/manager';
1313
import { EMPTY_ARGS, templateOnlyComponent, TemplateOnlyComponentManager } from '@glimmer/runtime';
1414
import { assign, expect } from '@glimmer/util';
1515

@@ -66,9 +66,27 @@ class DebugRenderTreeDelegate extends JitRenderDelegate {
6666

6767
this.registry.register('component', name, definition);
6868
}
69+
70+
registerCustomModifier(name: string) {
71+
const r = setModifierManager(
72+
() => ({
73+
capabilities: modifierCapabilities('3.22'),
74+
createModifier() {},
75+
installModifier() {},
76+
updateModifier() {},
77+
destroyModifier() {},
78+
}),
79+
class DidInsertModifier {}
80+
);
81+
this.registry.register('modifier', name, r);
82+
return r;
83+
}
6984
}
7085

7186
class DebugRenderTreeTest extends RenderTest {
87+
defineModifier(name: string) {
88+
return this.delegate.registerCustomModifier(name);
89+
}
7290
static suiteName = 'Application test: debug render tree';
7391

7492
declare delegate: DebugRenderTreeDelegate;
@@ -253,6 +271,87 @@ class DebugRenderTreeTest extends RenderTest {
253271
]);
254272
}
255273

274+
@test modifiers() {
275+
this.registerComponent('Glimmer', 'HelloWorld', 'Hello World');
276+
const didInsert = () => null;
277+
this.registerModifier(
278+
'did-insert',
279+
class {
280+
element?: SimpleElement;
281+
didInsertElement() {}
282+
didUpdate() {}
283+
willDestroyElement() {}
284+
}
285+
);
286+
const modifier = this.defineModifier('did-update');
287+
288+
this.render(
289+
`<div {{on 'click' this.didInsert}} {{did-insert this.didInsert}} {{did-update this.didInsert}} {{this.modifier this.didInsert}}><HelloWorld /></div>`,
290+
{
291+
didInsert: didInsert,
292+
modifier: modifier,
293+
}
294+
);
295+
296+
this.assertRenderTree([
297+
{
298+
type: 'html-element',
299+
name: 'div',
300+
args: { positional: [], named: {} },
301+
instance: (instance: HTMLDivElement) => instance.tagName === 'DIV',
302+
template: null,
303+
bounds: this.nodeBounds(this.element.firstChild),
304+
children: [
305+
{
306+
type: 'modifier',
307+
name: 'did-insert',
308+
args: { positional: [didInsert], named: {} },
309+
instance: (instance: any) => typeof instance.didInsertElement === 'function',
310+
template: null,
311+
bounds: this.nodeBounds(this.element.firstChild),
312+
children: [],
313+
},
314+
{
315+
type: 'modifier',
316+
name: 'did-update',
317+
args: { positional: [didInsert], named: {} },
318+
instance: (instance: any) => typeof instance.installModifier === 'function',
319+
template: null,
320+
bounds: this.nodeBounds(this.element.firstChild),
321+
children: [],
322+
},
323+
{
324+
type: 'modifier',
325+
name: 'DidInsertModifier',
326+
args: { positional: [didInsert], named: {} },
327+
instance: (instance: any) => typeof instance.installModifier === 'function',
328+
template: null,
329+
bounds: this.nodeBounds(this.element.firstChild),
330+
children: [],
331+
},
332+
{
333+
type: 'modifier',
334+
name: 'on',
335+
args: { positional: ['click', didInsert], named: {} },
336+
instance: (instance: any) => instance === undefined,
337+
template: null,
338+
bounds: this.nodeBounds(this.element.firstChild),
339+
children: [],
340+
},
341+
{
342+
type: 'component',
343+
name: 'HelloWorld',
344+
args: { positional: [], named: {} },
345+
instance: (instance: any) => instance !== undefined,
346+
template: '(unknown template module)',
347+
bounds: this.nodeBounds(this.element.firstChild!.firstChild),
348+
children: [],
349+
},
350+
],
351+
},
352+
]);
353+
}
354+
256355
@test 'getDebugCustomRenderTree works'() {
257356
let bucket1 = {};
258357
let instance1 = {};
@@ -502,7 +601,7 @@ class DebugRenderTreeTest extends RenderTest {
502601
this.assertRenderNode(actualNode, expected, `${actualNode.type}:${actualNode.name}`);
503602
});
504603
} else {
505-
this.assert.deepEqual(actual, [], path);
604+
this.assert.deepEqual(actual, expectedNodes, path);
506605
}
507606
}
508607

packages/@glimmer/interfaces/lib/runtime/debug-render-tree.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import type { SimpleElement, SimpleNode } from '@simple-dom/interface';
33
import type { Bounds } from '../dom/bounds.js';
44
import type { Arguments, CapturedArguments } from './arguments.js';
55

6-
export type RenderNodeType = 'outlet' | 'engine' | 'route-template' | 'component';
6+
export type RenderNodeType =
7+
| 'outlet'
8+
| 'engine'
9+
| 'route-template'
10+
| 'component'
11+
| 'html-element'
12+
| 'modifier';
713

814
export interface RenderNode {
915
type: RenderNodeType;

packages/@glimmer/runtime/lib/vm/element-builder.ts

+66-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
UpdatableBlock,
2121
} from '@glimmer/interfaces';
2222
import { destroy, registerDestructor } from '@glimmer/destroyable';
23+
import { createUnboundRef, REFERENCE } from '@glimmer/reference';
2324
import { assert, expect, Stack } from '@glimmer/util';
2425

2526
import type { DynamicAttribute } from './attributes/dynamic';
@@ -83,6 +84,7 @@ export class NewElementBuilder implements ElementBuilder {
8384
[CURSOR_STACK] = new Stack<Cursor>();
8485
private modifierStack = new Stack<Nullable<ModifierInstance[]>>();
8586
private blockStack = new Stack<LiveBlock>();
87+
private htmlElementsState: { shouldAddHtmlElement?: boolean }[];
8688

8789
static forInitialRender(env: Environment, cursor: CursorImpl) {
8890
return new this(env, cursor.element, cursor.nextSibling).initialize();
@@ -104,6 +106,7 @@ export class NewElementBuilder implements ElementBuilder {
104106
this.env = env;
105107
this.dom = env.getAppendOperations();
106108
this.updateOperations = env.getDOM();
109+
this.htmlElementsState = [];
107110
}
108111

109112
protected initialize(): this {
@@ -195,8 +198,8 @@ export class NewElementBuilder implements ElementBuilder {
195198
this.constructing = null;
196199
this.operations = null;
197200

198-
this.pushModifiers(modifiers);
199201
this.pushElement(element, null);
202+
this.pushModifiers(modifiers);
200203
this.didOpenElement(element);
201204
}
202205

@@ -205,6 +208,15 @@ export class NewElementBuilder implements ElementBuilder {
205208
}
206209

207210
closeElement(): Nullable<ModifierInstance[]> {
211+
const htmlState = this.htmlElementsState.pop();
212+
if (htmlState?.shouldAddHtmlElement) {
213+
const element = this.element;
214+
this.env.debugRenderTree?.didRender(htmlState, {
215+
parentElement: () => (element as any).parentElement,
216+
firstNode: () => element,
217+
lastNode: () => element,
218+
});
219+
}
208220
this.willCloseElement();
209221
this.popElement();
210222
return this.popModifiers();
@@ -247,6 +259,59 @@ export class NewElementBuilder implements ElementBuilder {
247259

248260
private pushModifiers(modifiers: Nullable<ModifierInstance[]>): void {
249261
this.modifierStack.push(modifiers);
262+
if (this.env.debugRenderTree) {
263+
modifiers = modifiers || [];
264+
const htmlState = {
265+
shouldAddHtmlElement:
266+
modifiers.length || (globalThis as any).ENV_DEBUG_RENDER_TREE_ALL_ELEMENTS,
267+
};
268+
this.htmlElementsState.push(htmlState);
269+
if (htmlState.shouldAddHtmlElement) {
270+
this.env.debugRenderTree?.create(htmlState, {
271+
type: 'html-element',
272+
name: this.element.tagName.toLowerCase(),
273+
args: {
274+
named: {},
275+
positional: [],
276+
} as any,
277+
instance: this.element,
278+
});
279+
}
280+
for (const modifier of modifiers) {
281+
const state = {};
282+
const name =
283+
modifier.definition.resolvedName ||
284+
(modifier.state as any).debugName ||
285+
'unknown-modifier';
286+
const instance = (modifier.state as any).instance || (modifier.state as any).delegate;
287+
const element = this.element;
288+
const args: any = {
289+
positional: [],
290+
named: {},
291+
};
292+
for (const value of (modifier.state as any).args.positional) {
293+
if (value && value[REFERENCE]) {
294+
args.positional.push(value);
295+
} else {
296+
args.positional.push(createUnboundRef(value, false));
297+
}
298+
}
299+
for (const [key, value] of Object.entries((modifier.state as any)?.args.named)) {
300+
args.named[key] = createUnboundRef(value, false);
301+
}
302+
this.env.debugRenderTree?.create(state, {
303+
type: 'modifier',
304+
name,
305+
args: args,
306+
instance,
307+
});
308+
this.env.debugRenderTree?.didRender(state, {
309+
parentElement: () => (element as any).parentElement,
310+
firstNode: () => element,
311+
lastNode: () => element,
312+
});
313+
}
314+
}
250315
}
251316

252317
private popModifiers(): Nullable<ModifierInstance[]> {

0 commit comments

Comments
 (0)