import './internal'; import { metadata } from 'aurelia-metadata'; import { Container } from './container'; import { DependencyCtorOrFunctor, ImplOrAny, Impl, Args, DependencyCtor, DependencyFunctor } from './types'; /** * Decorator: Specifies a custom Invoker for the decorated item. */ export function invoker, TArgs extends Args>( value: Invoker ): (target: DependencyCtor) => void { return target => { metadata.define(metadata.invoker, value, target); }; } /** * Decorator: Specifies that the decorated item should be called as a factory * function, rather than a constructor. */ export function invokeAsFactory, TArgs extends Args>( potentialTarget?: DependencyCtor ): void | ((target: DependencyCtor) => void) { const deco = (target: DependencyCtor) => { metadata.define(metadata.invoker, FactoryInvoker.instance, target); }; return potentialTarget ? deco(potentialTarget) : deco; } /** * A strategy for invoking a function, resulting in an object instance. */ export interface Invoker, TArgs extends Args> { /** * Invokes the function with the provided dependencies. * @param fn The constructor or factory function. * @param dependencies The dependencies of the function call. * @return The result of the function invocation. */ invoke( container: Container, fn: DependencyCtorOrFunctor, dependencies: TArgs ): ImplOrAny; /** * Invokes the function with the provided dependencies. * @param fn The constructor or factory function. * @param staticDependencies The static dependencies of the function. * @param dynamicDependencies Additional dependencies to use during * invocation. * @return The result of the function invocation. */ invokeWithDynamicDependencies( container: Container, fn: DependencyCtorOrFunctor, staticDependencies: TArgs[number][], dynamicDependencies: TArgs[number][] ): ImplOrAny; } /** * An Invoker that is used to invoke a factory method. */ export class FactoryInvoker< TBase, TImpl extends Impl = Impl, TArgs extends Args = Args > implements Invoker { /** * The singleton instance of the FactoryInvoker. */ public static instance: FactoryInvoker; /** * Invokes the function with the provided dependencies. * @param container The calling container. * @param fn The constructor or factory function. * @param dependencies The dependencies of the function call. * @return The result of the function invocation. */ public invoke( container: Container, fn: DependencyCtorOrFunctor, dependencies: TArgs ): ImplOrAny { let i = dependencies.length; const args = new Array(i); while (i--) { args[i] = container.get(dependencies[i]); } return ((fn as DependencyFunctor).apply(undefined, args as TArgs)) as ImplOrAny; } /** * Invokes the function with the provided dependencies. * @param container The calling container. * @param fn The constructor or factory function. * @param staticDependencies The static dependencies of the function. * @param dynamicDependencies Additional dependencies to use during invocation. * @return The result of the function invocation. */ public invokeWithDynamicDependencies( container: Container, fn: DependencyCtorOrFunctor, staticDependencies: TArgs[number][], dynamicDependencies: TArgs[number][] ): ImplOrAny { let i = staticDependencies.length; let args = new Array(i); while (i--) { args[i] = container.get(staticDependencies[i]); } if (dynamicDependencies !== undefined) { args = args.concat(dynamicDependencies); } return (fn as DependencyFunctor).apply(undefined, args as TArgs) as ImplOrAny; } } FactoryInvoker.instance = new FactoryInvoker();