UNPKG

jasc

Version:

Jasc Another Service Container

40 lines (39 loc) 1.89 kB
/** * A provider function is a function that takes a container where your dependencies are defined (at some point), and must return the services that you provide. * @template Services The services the provider should provide * @template Dependencies The dependencies the provider needs to define the `Services` */ export declare type ContainerProvider<Services, Dependencies = {}> = { (container: Container<Services, Dependencies>): Readonly<Services>; }; /** * * @template P The services the container should hold * @template Dependencies **DO NOT SET!* This template is only used internally for `ContainerProvider<Services, Dependencies>`'s */ export default class Container<P = { [name: string]: unknown; }, Dependencies = P> { private _tree; private _current; /** * Use a provider function that provides some of services defined in the template `<P>` * @param provider see `ContainerProvider` */ use<S, C extends this>(provider: ContainerProvider<S, Dependencies>): Readonly<C & S>; /** * Define a service for the container to serve. * The service will be defined as a property on the service, and will be lazily constructed. * The construction of the service takes place the first time it is resolved (read). * If a circular dependency is detected, an Error is thrown. * @param name Name of the service * @param factory The service factory * @returns The container * @throws {TypeError} if name is null, undefined or not a string, or if factory is null, undefined or not a function */ serve<T extends P[K], K extends keyof Omit<P, keyof this>, C extends this>(name: K, factory: (container: Readonly<Omit<P & Dependencies, K>>) => T): Readonly<C & Pick<P, K>>; /** * Dumps the loaded services to the console. */ dump(): this; }