Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Jul 12, 2024
1 parent eed14c5 commit a2f60fd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class TestModifierManager
return tag;
}

getDebugName(state: TestModifierDefinitionState, { Klass }: TestModifierDefinitionState) {
return Klass?.name || '<unknown>';
getDebugName(definition?: object) {
return (definition as any).instance.constructor.name || '<unknown>';
}

getDebugInstance({ instance }: TestModifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class FunctionalModifierManager implements ModifierManager<SimpleModifierState>
}
}

getDebugName(fn: Function) {
getDebugName(fn: SimpleModifierFn) {
return fn.name || '(anonymous function)';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/@glimmer/interfaces/lib/managers/modifier.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export interface ModifierManager<ModifierStateBucket> {
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;
}
47 changes: 23 additions & 24 deletions packages/@glimmer/manager/lib/public/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export function modifierCapabilities<Version extends keyof ModifierCapabilitiesV
});
}

export interface CustomModifierState<ModifierInstance> {
export interface CustomModifierState<ModifierStateBucket> {
tag: UpdatableTag;
element: SimpleElement;
modifier: ModifierInstance;
delegate: ModifierManager<ModifierInstance>;
modifier: ModifierStateBucket;
delegate: ModifierManager<ModifierStateBucket>;
args: Arguments;
debugName?: string;
definition?: object;
}

/**
Expand All @@ -65,12 +65,12 @@ export interface CustomModifierState<ModifierInstance> {
* `updateModifier()` - invoked when the arguments passed to a modifier change
* `destroyModifier()` - invoked when the modifier is about to be destroyed
*/
export class CustomModifierManager<O extends Owner, ModifierInstance>
implements InternalModifierManager<CustomModifierState<ModifierInstance>>
export class CustomModifierManager<O extends Owner, ModifierStateBucket>
implements InternalModifierManager<CustomModifierState<ModifierStateBucket>>
{
private componentManagerDelegates = new WeakMap<O, ModifierManager<ModifierInstance>>();
private componentManagerDelegates = new WeakMap<O, ModifierManager<ModifierStateBucket>>();

constructor(private factory: ManagerFactory<O, ModifierManager<ModifierInstance>>) {}
constructor(private factory: ManagerFactory<O, ModifierManager<ModifierStateBucket>>) {}

private getDelegateFor(owner: O) {
let { componentManagerDelegates } = this;
Expand Down Expand Up @@ -99,45 +99,44 @@ export class CustomModifierManager<O extends Owner, ModifierInstance>
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<ModifierInstance>;
let state: CustomModifierState<ModifierStateBucket>;

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<ModifierInstance>, definition: object) {
let delegate = state.delegate;
if (typeof delegate.getDebugName === 'function') {
getDebugName({ delegate, definition }: CustomModifierState<ModifierStateBucket>) {
if (typeof delegate?.getDebugName === 'function') {
return delegate.getDebugName(definition);
}
return (definition as any).name || '<unknown>';
}

getDebugInstance(state: CustomModifierState<ModifierInstance>) {
let delegate = state.delegate;
if (typeof delegate.getDebugInstance === 'function') {
return delegate.getDebugInstance(state);
getDebugInstance({ delegate, modifier }: CustomModifierState<ModifierStateBucket>) {
if (typeof delegate?.getDebugInstance === 'function') {
return delegate.getDebugInstance(modifier);
}
return state.modifier || delegate;
return modifier || delegate;
}

getTag({ tag }: CustomModifierState<ModifierInstance>) {
getTag({ tag }: CustomModifierState<ModifierStateBucket>) {
return tag;
}

install({ element, args, modifier, delegate }: CustomModifierState<ModifierInstance>) {
install({ element, args, modifier, delegate }: CustomModifierState<ModifierStateBucket>) {
let { capabilities } = delegate;

if (capabilities.disableAutoTracking === true) {
Expand All @@ -147,7 +146,7 @@ export class CustomModifierManager<O extends Owner, ModifierInstance>
}
}

update({ args, modifier, delegate }: CustomModifierState<ModifierInstance>) {
update({ args, modifier, delegate }: CustomModifierState<ModifierStateBucket>) {
let { capabilities } = delegate;

if (capabilities.disableAutoTracking === true) {
Expand All @@ -157,7 +156,7 @@ export class CustomModifierManager<O extends Owner, ModifierInstance>
}
}

getDestroyable(state: CustomModifierState<ModifierInstance>) {
getDestroyable(state: CustomModifierState<ModifierStateBucket>) {
return state;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@glimmer/runtime/lib/compiled/opcodes/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`);
Expand Down
4 changes: 2 additions & 2 deletions packages/@glimmer/runtime/lib/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a2f60fd

Please sign in to comment.