UNPKG

4.09 kBTypeScriptView Raw
1import { Token } from './token.class';
2import { Constructable } from './types/constructable.type';
3import { AbstractConstructable } from './types/abstract-constructable.type';
4import { ServiceIdentifier } from './types/service-identifier.type';
5import { ServiceMetadata } from './interfaces/service-metadata.interface';
6import { ServiceOptions } from './interfaces/service-options.interface';
7/**
8 * TypeDI can have multiple containers.
9 * One container is ContainerInstance.
10 */
11export declare class ContainerInstance {
12 /** Container instance id. */
13 readonly id: string;
14 /** All registered services in the container. */
15 private services;
16 constructor(id: string);
17 /**
18 * Checks if the service with given name or type is registered service container.
19 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
20 */
21 has<T>(type: Constructable<T>): boolean;
22 has<T>(id: string): boolean;
23 has<T>(id: Token<T>): boolean;
24 /**
25 * Retrieves the service with given name or type from the service container.
26 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
27 */
28 get<T>(type: Constructable<T>): T;
29 get<T>(type: AbstractConstructable<T>): T;
30 get<T>(id: string): T;
31 get<T>(id: Token<T>): T;
32 get<T>(id: ServiceIdentifier<T>): T;
33 /**
34 * Gets all instances registered in the container of the given service identifier.
35 * Used when service defined with multiple: true flag.
36 */
37 getMany<T>(type: Constructable<T>): T[];
38 getMany<T>(type: AbstractConstructable<T>): T[];
39 getMany<T>(id: string): T[];
40 getMany<T>(id: Token<T>): T[];
41 getMany<T>(id: ServiceIdentifier<T>): T[];
42 /**
43 * Sets a value for the given type or service name in the container.
44 */
45 set<T = unknown>(service: ServiceMetadata<T>): this;
46 set<T = unknown>(type: Constructable<T>, instance: T): this;
47 set<T = unknown>(type: AbstractConstructable<T>, instance: T): this;
48 set<T = unknown>(name: string, instance: T): this;
49 set<T = unknown>(token: Token<T>, instance: T): this;
50 set<T = unknown>(token: ServiceIdentifier, instance: T): this;
51 set<T = unknown>(metadata: ServiceOptions<T>): this;
52 set<T = unknown>(metadataArray: ServiceOptions<T>[]): this;
53 /**
54 * Removes services with a given service identifiers.
55 */
56 remove(identifierOrIdentifierArray: ServiceIdentifier | ServiceIdentifier[]): this;
57 /**
58 * Completely resets the container by removing all previously registered services from it.
59 */
60 reset(options?: {
61 strategy: 'resetValue' | 'resetServices';
62 }): this;
63 /**
64 * Returns all services registered with the given identifier.
65 */
66 private findAllServices;
67 /**
68 * Finds registered service in the with a given service identifier.
69 */
70 private findService;
71 /**
72 * Gets the value belonging to `serviceMetadata.id`.
73 *
74 * - if `serviceMetadata.value` is already set it is immediately returned
75 * - otherwise the requested type is resolved to the value saved to `serviceMetadata.value` and returned
76 */
77 private getServiceValue;
78 /**
79 * Initializes all parameter types for a given target service class.
80 */
81 private initializeParams;
82 /**
83 * Checks if given parameter type is primitive type or not.
84 */
85 private isPrimitiveParamType;
86 /**
87 * Applies all registered handlers on a given target class.
88 */
89 private applyPropertyHandlers;
90 /**
91 * Checks if the given service metadata contains a destroyable service instance and destroys it in place. If the service
92 * contains a callable function named `destroy` it is called but not awaited and the return value is ignored..
93 *
94 * @param serviceMetadata the service metadata containing the instance to destroy
95 * @param force when true the service will be always destroyed even if it's cannot be re-created
96 */
97 private destroyServiceInstance;
98}