Skip to content

Commit facb979

Browse files
committed
Cleanup modifier debug render tree implementation
1 parent d96fe0f commit facb979

File tree

6 files changed

+69
-38
lines changed

6 files changed

+69
-38
lines changed

packages/@glimmer-workspace/benchmark-env/lib/benchmark/on-modifier.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class OnModifierManager implements InternalModifierManager<OnModifierState, obje
2929
}
3030

3131
getDebugName() {
32-
return 'on-modifier';
32+
return 'on';
33+
}
34+
35+
getDebugInstance() {
36+
return null;
3337
}
3438

3539
install(state: OnModifierState) {

packages/@glimmer-workspace/integration-tests/lib/modifiers.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ export class TestModifierManager
4444
return tag;
4545
}
4646

47-
getDebugName() {
48-
return '<unknown>';
47+
getDebugName({ instance }: TestModifier) {
48+
if (instance && 'name' in instance && typeof instance.name === 'string') {
49+
return instance.name;
50+
} else {
51+
return '<unknown>';
52+
}
53+
}
54+
55+
getDebugInstance({ instance }: TestModifier) {
56+
return instance;
4957
}
5058

5159
install({ element, args, instance }: TestModifier) {

packages/@glimmer/interfaces/lib/managers/internal/modifier.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface InternalModifierManager<
2323
getTag(modifier: TModifierInstanceState): UpdatableTag | null;
2424

2525
getDebugName(Modifier: TModifierDefinitionState): string;
26+
getDebugInstance(Modifier: TModifierInstanceState): unknown;
2627

2728
// At initial render, the modifier gets a chance to install itself on the
2829
// element it is managing. It can also return a bucket of state that

packages/@glimmer/manager/lib/public/modifier.ts

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export class CustomModifierManager<O extends Owner, ModifierInstance>
125125
return debugName!;
126126
}
127127

128+
getDebugInstance({ modifier }: CustomModifierState<ModifierInstance>) {
129+
return modifier;
130+
}
131+
128132
getTag({ tag }: CustomModifierState<ModifierInstance>) {
129133
return tag;
130134
}

packages/@glimmer/runtime/lib/compiled/opcodes/component.ts

+33-35
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
} from '@glimmer/debug';
3838
import { registerDestructor } from '@glimmer/destroyable';
3939
import { managerHasCapability } from '@glimmer/manager';
40-
import { createUnboundRef, isConstRef, REFERENCE, valueForRef } from '@glimmer/reference';
40+
import { isConstRef, valueForRef } from '@glimmer/reference';
4141
import {
4242
assert,
4343
assign,
@@ -56,6 +56,7 @@ import type { UpdatingVM } from '../../vm';
5656
import type { InternalVM } from '../../vm/append';
5757
import type { BlockArgumentsImpl } from '../../vm/arguments';
5858

59+
import { ConcreteBounds } from '../../bounds';
5960
import { hasCustomDebugRenderTreeLifecycle } from '../../component/interfaces';
6061
import { resolveComponent } from '../../component/resolve';
6162
import { isCurriedType, isCurriedValue, resolveCurriedValue } from '../../curried-value';
@@ -491,44 +492,43 @@ export class ComponentElementOperations implements ElementOperations {
491492

492493
addModifier(vm: InternalVM, modifier: ModifierInstance, capturedArgs: CapturedArguments): void {
493494
this.modifiers.push(modifier);
494-
const env = vm.env;
495-
if (env.debugRenderTree) {
496-
const element = vm.elements().constructing!;
497-
const name =
498-
modifier.definition.resolvedName ||
499-
(modifier.state as any).debugName ||
500-
(modifier.definition.state as any).name ||
501-
'unknown-modifier';
502-
const instance = (modifier.state as any).delegate || modifier.manager;
503-
const args: any = {
504-
positional: [],
505-
named: {},
506-
};
507-
for (const value of capturedArgs.positional) {
508-
if (value && value[REFERENCE]) {
509-
args.positional.push(value);
510-
} else {
511-
args.positional.push(createUnboundRef(value, false));
512-
}
513-
}
514-
for (const [key, value] of Object.entries(capturedArgs.named)) {
515-
args.named[key] = createUnboundRef(value, false);
495+
496+
if (vm.env.debugRenderTree !== undefined) {
497+
const { manager, definition, state } = modifier;
498+
499+
// TODO: we need a stable object for the debugRenderTree as the key, add support for
500+
// the case where the state is a primitive, or if in practice we always have/require
501+
// an object, then change the internal types to reflect that
502+
if (state === null || (typeof state !== 'object' && typeof state !== 'function')) {
503+
return;
516504
}
517-
env.debugRenderTree.create(modifier.state as any, {
505+
506+
let { element, constructing } = vm.elements();
507+
let name = manager.getDebugName(definition);
508+
let instance = manager.getDebugInstance(state);
509+
510+
assert(constructing, `Expected a constructing element in addModifier`);
511+
512+
let bounds = new ConcreteBounds(element, constructing, constructing);
513+
514+
vm.env.debugRenderTree.create(state, {
518515
type: 'modifier',
519516
name,
520-
args,
517+
args: capturedArgs,
521518
instance,
522519
});
523-
env.debugRenderTree?.didRender(modifier.state as any, {
524-
parentElement: () => (element as any).parentElement,
525-
firstNode: () => element,
526-
lastNode: () => element,
520+
521+
vm.env.debugRenderTree.didRender(state, bounds);
522+
523+
// For tearing down the debugRenderTree
524+
vm.associateDestroyable(state);
525+
526+
vm.updateWith(new DebugRenderTreeUpdateOpcode(state));
527+
vm.updateWith(new DebugRenderTreeDidRenderOpcode(state, bounds));
528+
529+
registerDestructor(state, () => {
530+
vm.env.debugRenderTree?.willDestroy(state);
527531
});
528-
registerDestructor(
529-
modifier.state as any,
530-
() => vm.env.debugRenderTree?.willDestroy(modifier.state as any)
531-
);
532532
}
533533
}
534534

@@ -684,8 +684,6 @@ APPEND_OPCODES.add(Op.GetComponentSelf, (vm, { op1: _state, op2: _names }) => {
684684
instance: valueForRef(selfRef),
685685
});
686686

687-
vm.associateDestroyable(instance);
688-
689687
registerDestructor(instance, () => {
690688
vm.env.debugRenderTree?.willDestroy(instance);
691689
});

packages/@glimmer/runtime/lib/modifiers/on.ts

+16
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,22 @@ class OnModifierManager implements InternalModifierManager<OnModifierState, obje
312312
return 'on';
313313
}
314314

315+
getDebugInstance(state: OnModifierState): unknown {
316+
return {
317+
get eventName() {
318+
return state.listener?.eventName;
319+
},
320+
321+
get listener() {
322+
return state.listener?.userProvidedCallback;
323+
},
324+
325+
get options() {
326+
return state?.listener?.options;
327+
},
328+
};
329+
}
330+
315331
get counters(): { adds: number; removes: number } {
316332
return { adds, removes };
317333
}

0 commit comments

Comments
 (0)