Skip to content

Commit

Permalink
feat(alias): Provide alias functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonseydel committed Sep 9, 2019
1 parent efd9a4a commit 7dd9764
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/kernel/src/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ export class Resolver implements IResolver, IRegistration {
case ResolverStrategy.singleton:
case ResolverStrategy.transient:
return container.getFactory(this.state as Constructable);
case ResolverStrategy.alias:
const resolver = container.getResolver(this.state);
if (resolver == null || resolver.getFactory === void 0) {
return null;
}
return resolver.getFactory(container);
default:
return null;
}
Expand Down
41 changes: 38 additions & 3 deletions packages/runtime/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface IBuildInstruction {

export interface ITemplateDefinition extends IResourceDefinition {
cache?: '*' | number;
aliases?: string[];
template?: unknown;
instructions?: ITargetedInstruction[][];
dependencies?: Key[];
Expand Down Expand Up @@ -266,6 +267,7 @@ export class HooksDefinition {
class DefaultTemplateDefinition implements Required<ITemplateDefinition> {
public name: string;
public cache: '*' | number;
public aliases: string[];
public template: unknown;
public instructions: ITargetedInstruction[][];
public dependencies: IRegistry[];
Expand All @@ -290,6 +292,7 @@ class DefaultTemplateDefinition implements Required<ITemplateDefinition> {
this.instructions = PLATFORM.emptyArray as typeof PLATFORM.emptyArray & this['instructions'];
this.dependencies = PLATFORM.emptyArray as typeof PLATFORM.emptyArray & this['dependencies'];
this.surrogates = PLATFORM.emptyArray as typeof PLATFORM.emptyArray & this['surrogates'];
this.aliases = PLATFORM.emptyArray as typeof PLATFORM.emptyArray & this['aliases'];
this.containerless = false;
this.shadowOptions = null!;
this.hasSlots = false;
Expand All @@ -312,13 +315,15 @@ const templateDefinitionAssignables = [
const templateDefinitionArrays = [
'instructions',
'dependencies',
'surrogates'
'surrogates',
'aliases'
];

export type CustomElementConstructor = Constructable & {
containerless?: TemplateDefinition['containerless'];
shadowOptions?: TemplateDefinition['shadowOptions'];
bindables?: TemplateDefinition['bindables'];
aliases?: string[];
childrenObservers?: TemplateDefinition['childrenObservers'];
};

Expand Down Expand Up @@ -347,7 +352,9 @@ export function buildTemplateDefinition(
shadowOptions?: { mode: 'open' | 'closed' } | null,
hasSlots?: boolean | null,
strategy?: BindingStrategy | null,
childrenObservers?: Record<string, IChildrenObserverDescription> | null): TemplateDefinition;
childrenObservers?: Record<string, IChildrenObserverDescription> | null,
aliases?: ReadonlyArray<string> | null,
): TemplateDefinition;
// tslint:disable-next-line:parameters-max-number // TODO: Reduce complexity (currently at 64)
export function buildTemplateDefinition(
ctor: CustomElementConstructor | null,
Expand All @@ -363,13 +370,16 @@ export function buildTemplateDefinition(
shadowOptions?: { mode: 'open' | 'closed' } | null,
hasSlots?: boolean | null,
strategy?: BindingStrategy | null,
childrenObservers?: Record<string, IChildrenObserverDescription> | null): TemplateDefinition {
childrenObservers?: Record<string, IChildrenObserverDescription> | null,
aliases?: ReadonlyArray<string> | null,
): TemplateDefinition {

const def = new DefaultTemplateDefinition();

// all cases fall through intentionally
const argLen = arguments.length;
switch (argLen) {
case 15: if (aliases != null) def.aliases = toArray(aliases);
case 14: if (childrenObservers !== null) def.childrenObservers = { ...childrenObservers };
case 13: if (strategy != null) def.strategy = ensureValidStrategy(strategy);
case 12: if (hasSlots != null) def.hasSlots = hasSlots!;
Expand Down Expand Up @@ -442,3 +452,28 @@ export function buildTemplateDefinition(

return def;
}



type HasAliases = Pick<ITemplateDefinition | IAttributeDefinition, 'aliases'>;

function aliasesDecorator<T extends Constructable>(target: T & HasAliases, ...aliases: string[]): T & Required<HasAliases> {
target.aliases = aliases;
return target as T & Required<HasAliases>;
}

/**
* Decorator: Indicates that the custom element should be rendered without its element container.
*/
export function aliases(): typeof aliasesDecorator;
/**
* Decorator: Indicates that the custom element should be rendered without its element container.
*/
export function aliases<T extends Constructable>(...aliases: string[]): T & Required<HasAliases>;
export function aliases<T extends Constructable>(target?: T & HasAliases | string[], ...aliases: string[]): T & Required<HasAliases> | typeof aliasesDecorator {

if (target !== null && (typeof target === 'string' || Array.isArray(target))) {
return (instance: any) => aliasesDecorator(instance, ...aliases);
}
return target === undefined ? aliasesDecorator : aliasesDecorator<T>(target, ...aliases);
}
8 changes: 7 additions & 1 deletion packages/runtime/src/resources/custom-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface ICustomElementStaticProperties {
shadowOptions?: TemplateDefinition['shadowOptions'];
bindables?: TemplateDefinition['bindables'];
strategy?: TemplateDefinition['strategy'];
aliases?: TemplateDefinition['aliases'];
}

export interface ICustomElementResource<T extends INode = INode> extends
Expand Down Expand Up @@ -97,9 +98,14 @@ export const CustomElement: Readonly<ICustomElementResource> = Object.freeze({
WritableType.kind = CustomElement;
Type.description = description;
Type.register = function register(container: IContainer): void {
const aliases = description.aliases;
const key = CustomElement.keyFrom(description.name);
Registration.transient(key, this).register(container);
Registration.alias(key, this).register(container);

for (let i = 0, ii = aliases.length; i < ii; ++i) {
Registration.alias(key, CustomElement.keyFrom(aliases[i])).register(container);
}
};

return Type;
Expand Down Expand Up @@ -156,4 +162,4 @@ export function containerless(): typeof containerlessDecorator;
export function containerless<T extends Constructable>(target: T & HasContainerless): T & Required<HasContainerless>;
export function containerless<T extends Constructable>(target?: T & HasContainerless): T & Required<HasContainerless> | typeof containerlessDecorator {
return target === undefined ? containerlessDecorator : containerlessDecorator<T>(target);
}
}

0 comments on commit 7dd9764

Please sign in to comment.