Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add modifiers to debug render tree #1559

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class OnModifierManager implements InternalModifierManager<OnModifierState, obje
}

getDebugName() {
return 'on-modifier';
return 'on';
}

getDebugInstance() {
return null;
}

install(state: OnModifierState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export class TestModifierManager
return tag;
}

getDebugName() {
return '<unknown>';
getDebugName({ Klass }: TestModifierDefinitionState) {
return Klass?.name || '<unknown>';
}

getDebugInstance({ instance }: TestModifier) {
return instance;
}

install({ element, args, instance }: TestModifier) {
Expand Down
13 changes: 13 additions & 0 deletions packages/@glimmer-workspace/integration-tests/lib/setup-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ export async function setupQunit() {
qunit.moduleDone(pause);
}

// @ts-expect-error missing in types, does exist: https://api.qunitjs.com/callbacks/QUnit.on/#the-testend-event
QUnit.on('testEnd', (testEnd) => {
if (testEnd.status === 'failed') {
testEnd.errors.forEach((assertion: any) => {
console.error(assertion.stack);
Copy link
Contributor Author

@patricklx patricklx Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prints error stack traces from tests to console, useful for debugging, like clicking on error location to jump to the code

// message: speedometer
// actual: 75
// expected: 88
// stack: at dmc.test.js:12
});
}
});

qunit.done(({ failed }) => {
if (failed > 0) {
console.log('[HARNESS] fail');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
SimpleNode,
} from '@glimmer/interfaces';
import type { TemplateOnlyComponent } from '@glimmer/runtime';
import { setComponentTemplate } from '@glimmer/manager';
import { modifierCapabilities, setComponentTemplate, setModifierManager } from '@glimmer/manager';
import { EMPTY_ARGS, templateOnlyComponent, TemplateOnlyComponentManager } from '@glimmer/runtime';
import { assign, expect } from '@glimmer/util';

Expand Down Expand Up @@ -331,6 +331,188 @@ class DebugRenderTreeTest extends RenderTest {
]);
}

@test modifiers() {
this.registerComponent('Glimmer', 'HelloWorld', 'Hello World');
const didInsert = () => null;

class DidInsertModifier {
element?: SimpleElement;
didInsertElement() {}
didUpdate() {}
willDestroyElement() {}
}

this.registerModifier('did-insert', DidInsertModifier);

class MyCustomModifier {}

setModifierManager(
() => ({
capabilities: modifierCapabilities('3.22'),
createModifier() {
return new MyCustomModifier();
},
installModifier() {},
updateModifier() {},
destroyModifier() {},
}),
MyCustomModifier
);

const foo = Symbol('foo');
const bar = Symbol('bar');

this.render(
`<div {{on 'click' this.didInsert}} {{did-insert this.foo bar=this.bar}} {{this.modifier this.bar foo=this.foo}}
><HelloWorld />
{{~#if this.more~}}
<div {{on 'click' this.didInsert passive=true}}></div>
{{~/if~}}
</div>`,
{
didInsert: didInsert,
modifier: MyCustomModifier,
foo,
bar,
more: false,
}
);

this.assertRenderTree([
{
type: 'modifier',
name: 'on',
args: { positional: ['click', didInsert], named: {} },
instance: null,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'modifier',
name: 'DidInsertModifier',
args: { positional: [foo], named: { bar } },
instance: (instance: unknown) => instance instanceof DidInsertModifier,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'modifier',
name: 'MyCustomModifier',
args: { positional: [bar], named: { foo } },
instance: (instance: unknown) => instance instanceof MyCustomModifier,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'component',
name: 'HelloWorld',
args: { positional: [], named: {} },
instance: (instance: any) => instance !== undefined,
template: '(unknown template module)',
bounds: this.nodeBounds(this.element.firstChild!.firstChild),
children: [],
},
]);

this.rerender({
more: true,
});

this.assertRenderTree([
{
type: 'modifier',
name: 'on',
args: { positional: ['click', didInsert], named: {} },
instance: null,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'modifier',
name: 'DidInsertModifier',
args: { positional: [foo], named: { bar } },
instance: (instance: unknown) => instance instanceof DidInsertModifier,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'modifier',
name: 'MyCustomModifier',
args: { positional: [bar], named: { foo } },
instance: (instance: unknown) => instance instanceof MyCustomModifier,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'component',
name: 'HelloWorld',
args: { positional: [], named: {} },
instance: (instance: any) => instance !== undefined,
template: '(unknown template module)',
bounds: this.nodeBounds(this.element.firstChild!.firstChild),
children: [],
},
{
type: 'modifier',
name: 'on',
args: { positional: ['click', didInsert], named: { passive: true } },
instance: null,
template: null,
bounds: this.nodeBounds(this.element.firstChild!.lastChild),
children: [],
},
]);

this.rerender({
more: false,
});

this.assertRenderTree([
{
type: 'modifier',
name: 'on',
args: { positional: ['click', didInsert], named: {} },
instance: null,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'modifier',
name: 'DidInsertModifier',
args: { positional: [foo], named: { bar } },
instance: (instance: unknown) => instance instanceof DidInsertModifier,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'modifier',
name: 'MyCustomModifier',
args: { positional: [bar], named: { foo } },
instance: (instance: unknown) => instance instanceof MyCustomModifier,
template: null,
bounds: this.nodeBounds(this.element.firstChild),
children: [],
},
{
type: 'component',
name: 'HelloWorld',
args: { positional: [], named: {} },
instance: (instance: any) => instance !== undefined,
template: '(unknown template module)',
bounds: this.nodeBounds(this.element.firstChild!.firstChild),
children: [],
},
]);
}

@test 'getDebugCustomRenderTree works'() {
let bucket1 = {};
let instance1 = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface InternalModifierManager<
getTag(modifier: TModifierInstanceState): UpdatableTag | null;

getDebugName(Modifier: TModifierDefinitionState): string;
getDebugInstance(Modifier: TModifierInstanceState): unknown;

// At initial render, the modifier gets a chance to install itself on the
// element it is managing. It can also return a bucket of state that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import type { SimpleElement, SimpleNode } from '@simple-dom/interface';
import type { Bounds } from '../dom/bounds.js';
import type { Arguments, CapturedArguments } from './arguments.js';

export type RenderNodeType = 'outlet' | 'engine' | 'route-template' | 'component' | 'keyword';
export type RenderNodeType =
| 'outlet'
| 'engine'
| 'route-template'
| 'component'
| 'modifier'
| 'keyword';

export interface RenderNode {
type: RenderNodeType;
Expand Down
16 changes: 10 additions & 6 deletions packages/@glimmer/manager/lib/public/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,21 @@ export class CustomModifierManager<O extends Owner, ModifierInstance>
modifier: instance,
};

if (import.meta.env.DEV) {
state.debugName = typeof definition === 'function' ? definition.name : definition.toString();
}

registerDestructor(state, () => delegate.destroyModifier(instance, args));

return state;
}

getDebugName({ debugName }: CustomModifierState<ModifierInstance>) {
return debugName!;
getDebugName(definition: object) {
if (typeof definition === 'function') {
return definition.name || definition.toString();
} else {
return '<unknown>';
}
}

getDebugInstance({ modifier }: CustomModifierState<ModifierInstance>) {
return modifier;
}

getTag({ tag }: CustomModifierState<ModifierInstance>) {
Expand Down
4 changes: 4 additions & 0 deletions packages/@glimmer/manager/test/managers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ module('Managers', () => {
return 'internal';
}

getDebugInstance() {
return null;
}

getDestroyable() {
return null;
}
Expand Down
43 changes: 40 additions & 3 deletions packages/@glimmer/runtime/lib/compiled/opcodes/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import type { UpdatingVM } from '../../vm';
import type { InternalVM } from '../../vm/append';
import type { BlockArgumentsImpl } from '../../vm/arguments';

import { ConcreteBounds } from '../../bounds';
import { hasCustomDebugRenderTreeLifecycle } from '../../component/interfaces';
import { resolveComponent } from '../../component/resolve';
import { isCurriedType, isCurriedValue, resolveCurriedValue } from '../../curried-value';
Expand Down Expand Up @@ -489,8 +490,46 @@ export class ComponentElementOperations implements ElementOperations {
this.attributes[name] = deferred;
}

addModifier(modifier: ModifierInstance): void {
addModifier(vm: InternalVM, modifier: ModifierInstance, capturedArgs: CapturedArguments): void {
this.modifiers.push(modifier);

if (vm.env.debugRenderTree !== undefined) {
const { manager, definition, state } = modifier;

// TODO: we need a stable object for the debugRenderTree as the key, add support for
// the case where the state is a primitive, or if in practice we always have/require
// an object, then change the internal types to reflect that
if (state === null || (typeof state !== 'object' && typeof state !== 'function')) {
return;
}

let { element, constructing } = vm.elements();
let name = manager.getDebugName(definition.state);
let instance = manager.getDebugInstance(state);

assert(constructing, `Expected a constructing element in addModifier`);

let bounds = new ConcreteBounds(element, constructing, constructing);

vm.env.debugRenderTree.create(state, {
type: 'modifier',
name,
args: capturedArgs,
instance,
});

vm.env.debugRenderTree.didRender(state, bounds);

// For tearing down the debugRenderTree
vm.associateDestroyable(state);

vm.updateWith(new DebugRenderTreeUpdateOpcode(state));
vm.updateWith(new DebugRenderTreeDidRenderOpcode(state, bounds));

registerDestructor(state, () => {
vm.env.debugRenderTree?.willDestroy(state);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of these are analogous to the component equivalent in the same file

}

flush(vm: InternalVM): ModifierInstance[] {
Expand Down Expand Up @@ -645,8 +684,6 @@ APPEND_OPCODES.add(Op.GetComponentSelf, (vm, { op1: _state, op2: _names }) => {
instance: valueForRef(selfRef),
});

vm.associateDestroyable(instance);

registerDestructor(instance, () => {
vm.env.debugRenderTree?.willDestroy(instance);
});
Expand Down
Loading
Loading