Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Container<Items, Resolvers>

Type parameters

Hierarchy

  • Container

Implements

Index

Constructors

Properties

cache: Map<keyof Items, any> = ...
events: Emittery<ContainerEventsPayload, ContainerEventsPayload & _OmnipresentEventData, never> = ...

Emits container related events.

example
const container = Container.create();

container.events.on(ContainerEvents.disposed, () => {
console.log('Container was disposed');
});
id: string = ...

Unique id for this container

items: Items
options: Required<ContainerOptions>

Accessors

  • get containerResolvers(): Readonly<Resolvers>

Methods

  • build<Key>(key: Key, params?: Omit<ResolveParams<Resolvers, Key>, "omitCache">): any
  • Builds given resolver, but doesn't cache it, meaning that configured lifetime won't take effect here

    Type parameters

    • Key: string | number | symbol

    Parameters

    • key: Key
    • Optional params: Omit<ResolveParams<Resolvers, Key>, "omitCache">

    Returns any

  • clear(): Promise<void>
  • It clears the container cache and resets the resolvers.

    Returns Promise<void>

  • clearCache(silent?: boolean): Promise<void>
  • It clears the cache of all resolvers that are not singletons

    Parameters

    • silent: boolean = false

    Returns Promise<void>

  • createProxy(additionalItems?: Partial<Items>): Items
  • Returns a proxy object that resolves the requested property from the container, or from the additionalItems object if it's provided

    Parameters

    • Optional additionalItems: Partial<Items>

    Returns Items

    A proxy object that has a getter that returns the resolved value of the key.

  • Creates scoped instance of current container. All items that are registered with lifetime of LifeTime.Scoped will be resolved once for created scope. Singleton items will be resolved from root container.

    Returns Container<Items, ResolversMap>

    A new Container instance.

  • declare<Key, Type>(key: Key): Container<Items & Record<Key, Type & { [declarationSymbol]?: true }>, Resolvers & Record<Key, Resolver<any, any>>>
  • Takes a key and a type, and returns a new container type with the key and type added to it. Does not register anything, but only adds the type to the container that can be used and registered later.

    example
    const container = Container
    .create()
    .declare<'now', Date>('now')
    .register({
    key: 'tomorrow',
    factory: store => new Date(store.now.getTime() + 86400000),
    })
    .register({
    key: 'now',
    // Whoops! TypeScript will fail here, because the expected type is "Date"!
    factory: () => 'now'
    });

    Type parameters

    Parameters

    • key: Key

    Returns Container<Items & Record<Key, Type & { [declarationSymbol]?: true }>, Resolvers & Record<Key, Resolver<any, any>>>

    The container itself, but with the new type added to the items.

  • dispose(silent?: boolean): Promise<void>
  • Fully disposes container instance, clearing cache, removing children containers and clearing resolvers.

    Parameters

    • silent: boolean = false

    Returns Promise<void>

  • has<Key>(key: Key): boolean
  • Checks if the resolver for the given key exists. Does not actually resolve it.

    Type parameters

    • Key: string | number | symbol

    Parameters

    • key: Key

    Returns boolean

  • Registers given resolver.

    example

    const container = Container.create().register({
    key: 'now',
    factory: () => new Date(),
    lifetime: LifeTime.Scoped
    });

    console.log(container.items.now.toISOString()); // 2020-01-01T00:00:00.000Z

    Type parameters

    • Key: string | number | symbol

    • T

    Parameters

    Returns Container<Items & Record<Key, T>, Resolvers & Record<Key, Resolver<ResolverParamsValue<Key, Items, T>, Items>>>

    Container with extended type which contains registered resolver.

  • Takes a record of resolvers, and returns a container with the resolvers registered. Use to register many resolvers that don't depend on one another.

    example

    const container = Container.create().registerMany({
    number: {
    factory: () => Math.random(),
    },
    now: {
    factory: () => Date.now(),
    }
    });

    console.log(container.items.number); // number
    console.log(container.items.now); // Date

    Type parameters

    Parameters

    • record: T

    Returns Container<Items & ResolvedResolversRecord<T>, Resolvers & ResolversFromResolversRecord<T>>

    Container with extended type which contains registered resolvers.

  • resolve<Key>(key: Key, params?: ResolveParams<Resolvers, Key>): Items[Key]
  • Resolved items stored inside container.

    example
    const container = Container
    .create()
    .register({
    key: 'now',
    factory: () => new Date(),
    })
    .register({
    key: 'tomorrow',
    factory: store => {
    const tomorrow = new Date(store.now);

    tomorrow.setDate(tomorrow.getDate() + 1);

    return tomorrow;
    }
    });

    console.log(container.resolve('tomorrow').toISOString()); // 2020-01-01T00:00:00.000Z

    console.log(container.resolve('tomorrow', {
    injectionParams: {
    // Provide custom params that will be injected while resolving item
    now: new Date(),
    },
    }).toISOString()); // 2020-01-01T00:00:00.000Z

    Type parameters

    • Key: string | number | symbol

    Parameters

    Returns Items[Key]

    Resolved item from container.

Generated using TypeDoc