From a2f60fdb301056240c58781800eaf58753ecc955 Mon Sep 17 00:00:00 2001 From: patrickpircher Date: Thu, 11 Jul 2024 23:21:54 +0200 Subject: [PATCH] cleanup code --- .../integration-tests/lib/modifiers.ts | 4 +- .../lib/test-helpers/define.ts | 2 +- .../lib/managers/internal/modifier.d.ts | 4 +- .../interfaces/lib/managers/modifier.d.ts | 4 +- .../@glimmer/manager/lib/public/modifier.ts | 47 +++++++++---------- .../runtime/lib/compiled/opcodes/component.ts | 4 +- packages/@glimmer/runtime/lib/environment.ts | 4 +- 7 files changed, 34 insertions(+), 35 deletions(-) diff --git a/packages/@glimmer-workspace/integration-tests/lib/modifiers.ts b/packages/@glimmer-workspace/integration-tests/lib/modifiers.ts index d255d6fe9d..a749439c65 100644 --- a/packages/@glimmer-workspace/integration-tests/lib/modifiers.ts +++ b/packages/@glimmer-workspace/integration-tests/lib/modifiers.ts @@ -44,8 +44,8 @@ export class TestModifierManager return tag; } - getDebugName(state: TestModifierDefinitionState, { Klass }: TestModifierDefinitionState) { - return Klass?.name || ''; + getDebugName(definition?: object) { + return (definition as any).instance.constructor.name || ''; } getDebugInstance({ instance }: TestModifier) { diff --git a/packages/@glimmer-workspace/integration-tests/lib/test-helpers/define.ts b/packages/@glimmer-workspace/integration-tests/lib/test-helpers/define.ts index 74e7ac7ee9..28633885d6 100644 --- a/packages/@glimmer-workspace/integration-tests/lib/test-helpers/define.ts +++ b/packages/@glimmer-workspace/integration-tests/lib/test-helpers/define.ts @@ -82,7 +82,7 @@ class FunctionalModifierManager implements ModifierManager } } - getDebugName(fn: Function) { + getDebugName(fn: SimpleModifierFn) { return fn.name || '(anonymous function)'; } } diff --git a/packages/@glimmer/interfaces/lib/managers/internal/modifier.d.ts b/packages/@glimmer/interfaces/lib/managers/internal/modifier.d.ts index 7aea472c90..d49064171b 100644 --- a/packages/@glimmer/interfaces/lib/managers/internal/modifier.d.ts +++ b/packages/@glimmer/interfaces/lib/managers/internal/modifier.d.ts @@ -22,8 +22,8 @@ export interface InternalModifierManager< // the modifier's update hooks need to be called (if at all). getTag(modifier: TModifierInstanceState): UpdatableTag | null; - getDebugName(Modifier: TModifierDefinitionState, definition: object): string; - getDebugInstance(Modifier: TModifierInstanceState): unknown; + getDebugName(modifier: TModifierInstanceState): 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 diff --git a/packages/@glimmer/interfaces/lib/managers/modifier.d.ts b/packages/@glimmer/interfaces/lib/managers/modifier.d.ts index fc014ada5f..bf5e7f10a7 100644 --- a/packages/@glimmer/interfaces/lib/managers/modifier.d.ts +++ b/packages/@glimmer/interfaces/lib/managers/modifier.d.ts @@ -19,6 +19,6 @@ export interface ModifierManager { installModifier(instance: ModifierStateBucket, element: Element, args: Arguments): void; updateModifier(instance: ModifierStateBucket, args: Arguments): void; destroyModifier(instance: ModifierStateBucket, args: Arguments): void; - getDebugName?(instance: object): string; - getDebugInstance?(instance: object): any; + getDebugName?(definition?: object): string; + getDebugInstance?(instance: ModifierStateBucket): any; } diff --git a/packages/@glimmer/manager/lib/public/modifier.ts b/packages/@glimmer/manager/lib/public/modifier.ts index 6d864b7446..460bbbdc79 100644 --- a/packages/@glimmer/manager/lib/public/modifier.ts +++ b/packages/@glimmer/manager/lib/public/modifier.ts @@ -32,13 +32,13 @@ export function modifierCapabilities { +export interface CustomModifierState { tag: UpdatableTag; element: SimpleElement; - modifier: ModifierInstance; - delegate: ModifierManager; + modifier: ModifierStateBucket; + delegate: ModifierManager; args: Arguments; - debugName?: string; + definition?: object; } /** @@ -65,12 +65,12 @@ export interface CustomModifierState { * `updateModifier()` - invoked when the arguments passed to a modifier change * `destroyModifier()` - invoked when the modifier is about to be destroyed */ -export class CustomModifierManager - implements InternalModifierManager> +export class CustomModifierManager + implements InternalModifierManager> { - private componentManagerDelegates = new WeakMap>(); + private componentManagerDelegates = new WeakMap>(); - constructor(private factory: ManagerFactory>) {} + constructor(private factory: ManagerFactory>) {} private getDelegateFor(owner: O) { let { componentManagerDelegates } = this; @@ -99,45 +99,44 @@ export class CustomModifierManager let delegate = this.getDelegateFor(owner); let args = argsProxyFor(capturedArgs, 'modifier'); - let instance: ModifierInstance = delegate.createModifier(definition, args); + let modifier: ModifierStateBucket = delegate.createModifier(definition, args); let tag = createUpdatableTag(); - let state: CustomModifierState; + let state: CustomModifierState; state = { tag, element, delegate, args, - modifier: instance, + definition, + modifier, }; - registerDestructor(state, () => delegate.destroyModifier(instance, args)); + registerDestructor(state, () => delegate.destroyModifier(modifier, args)); return state; } - getDebugName(state: CustomModifierState, definition: object) { - let delegate = state.delegate; - if (typeof delegate.getDebugName === 'function') { + getDebugName({ delegate, definition }: CustomModifierState) { + if (typeof delegate?.getDebugName === 'function') { return delegate.getDebugName(definition); } return (definition as any).name || ''; } - getDebugInstance(state: CustomModifierState) { - let delegate = state.delegate; - if (typeof delegate.getDebugInstance === 'function') { - return delegate.getDebugInstance(state); + getDebugInstance({ delegate, modifier }: CustomModifierState) { + if (typeof delegate?.getDebugInstance === 'function') { + return delegate.getDebugInstance(modifier); } - return state.modifier || delegate; + return modifier || delegate; } - getTag({ tag }: CustomModifierState) { + getTag({ tag }: CustomModifierState) { return tag; } - install({ element, args, modifier, delegate }: CustomModifierState) { + install({ element, args, modifier, delegate }: CustomModifierState) { let { capabilities } = delegate; if (capabilities.disableAutoTracking === true) { @@ -147,7 +146,7 @@ export class CustomModifierManager } } - update({ args, modifier, delegate }: CustomModifierState) { + update({ args, modifier, delegate }: CustomModifierState) { let { capabilities } = delegate; if (capabilities.disableAutoTracking === true) { @@ -157,7 +156,7 @@ export class CustomModifierManager } } - getDestroyable(state: CustomModifierState) { + getDestroyable(state: CustomModifierState) { return state; } } diff --git a/packages/@glimmer/runtime/lib/compiled/opcodes/component.ts b/packages/@glimmer/runtime/lib/compiled/opcodes/component.ts index 332cdd56a9..a38816d8d3 100644 --- a/packages/@glimmer/runtime/lib/compiled/opcodes/component.ts +++ b/packages/@glimmer/runtime/lib/compiled/opcodes/component.ts @@ -494,7 +494,7 @@ export class ComponentElementOperations implements ElementOperations { this.modifiers.push(modifier); if (vm.env.debugRenderTree !== undefined) { - const { manager, definition, state } = modifier; + const { manager, 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 @@ -504,7 +504,7 @@ export class ComponentElementOperations implements ElementOperations { } let { element, constructing } = vm.elements(); - let name = manager.getDebugName(state, definition.state); + let name = manager.getDebugName(state); let instance = manager.getDebugInstance(state); assert(constructing, `Expected a constructing element in addModifier`); diff --git a/packages/@glimmer/runtime/lib/environment.ts b/packages/@glimmer/runtime/lib/environment.ts index d132c21b56..52ab7d11bc 100644 --- a/packages/@glimmer/runtime/lib/environment.ts +++ b/packages/@glimmer/runtime/lib/environment.ts @@ -65,7 +65,7 @@ class TransactionImpl implements Transaction { () => manager.install(state), import.meta.env.DEV && `- While rendering:\n (instance of a \`${ - definition.resolvedName || manager.getDebugName(state as any, definition.state) + definition.resolvedName || manager.getDebugName(state) }\` modifier)` ); updateTag(modifierTag, tag); @@ -82,7 +82,7 @@ class TransactionImpl implements Transaction { () => manager.update(state), import.meta.env.DEV && `- While rendering:\n (instance of a \`${ - definition.resolvedName || manager.getDebugName(state as any, definition.state) + definition.resolvedName || manager.getDebugName(state) }\` modifier)` ); updateTag(modifierTag, tag);