# All files / utils/container Container.ts

33.33% Statements 11/33
12.5% Branches 1/8
33.33% Functions 4/12
36.67% Lines 11/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199

    7x

    7x

    7x

    7x

    7x

    42x
    42x

    6x

    42x

    70x
    70x

     import { ServiceMetadata } from './types/ServiceMetadata';
    import { ObjectType } from './types/ObjectType';
    import { Handler } from './types/Handler';
    import { Token } from './Token';
    import { ServiceIdentifier } from './types/ServiceIdentifier';
    import { ContainerInstance } from './ContainerInstance';

    /**
     * Service container.
     */
    export class Container {

        // -------------------------------------------------------------------------
        // Private Static Properties
        // -------------------------------------------------------------------------

        /**
         * Global container instance.
         */
        private static readonly globalInstance: ContainerInstance = new ContainerInstance(undefined);

        /**
         * Other containers created using Container.of method.
         */
        private static readonly instances: ContainerInstance[] = [];

        /**
         * All registered handlers.
         */
        static readonly handlers: Handler[] = [];

        // -------------------------------------------------------------------------
        // Public Static Methods
        // -------------------------------------------------------------------------

        /**
         * Gets a separate container instance for the given instance id.
         */
        static of(instanceId: any): ContainerInstance {
            Eif (instanceId === undefined)
                return this.globalInstance;

            let container = this.instances.find(instance => instance.id === instanceId);
            if (!container) {
                container = new ContainerInstance(instanceId);
                this.instances.push(container);
            }

            return container;
        }

        /**
         * Checks if the service with given name or type is registered service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static has<T>(type: ObjectType<T>): boolean;

        /**
         * Checks if the service with given name or type is registered service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static has<T>(id: string): boolean;

        /**
         * Checks if the service with given name or type is registered service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static has<T>(id: Token<T>): boolean;

        /**
         * Checks if the service with given name or type is registered service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static has<T>(identifier: ServiceIdentifier): boolean {
            return this.globalInstance.has(identifier as any);
        }

        /**
         * Retrieves the service with given name or type from the service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static get<T>(type: ObjectType<T>): T;

        /**
         * Retrieves the service with given name or type from the service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static get<T>(id: string): T;

        /**
         * Retrieves the service with given name or type from the service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static get<T>(id: Token<T>): T;

        /**
         * Retrieves the service with given name or type from the service container.
         * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
         */
        static get<T>(identifier: ServiceIdentifier): T {
            return this.globalInstance.get(identifier as any);
        }

        /**
         * Gets all instances registered in the container of the given service identifier.
         * Used when service defined with multiple: true flag.
         */
        static getMany<T>(id: string): T[];

        /**
         * Gets all instances registered in the container of the given service identifier.
         * Used when service defined with multiple: true flag.
         */
        static getMany<T>(id: Token<T>): T[];

        /**
         * Gets all instances registered in the container of the given service identifier.
         * Used when service defined with multiple: true flag.
         */
        static getMany<T>(id: string | Token<T>): T[] {
            return this.globalInstance.getMany(id as any);
        }

        /**
         * Sets a value for the given type or service name in the container.
         */
        static set<T, K extends keyof T>(service: ServiceMetadata<T, K>): Container;

        /**
         * Sets a value for the given type or service name in the container.
         */
        static set(type: Function, value: any): Container;

        /**
         * Sets a value for the given type or service name in the container.
         */
        static set(name: string, value: any): Container;

        /**
         * Sets a value for the given type or service name in the container.
         */
        static set(token: Token<any>, value: any): Container;

        /**
         * Sets a value for the given type or service name in the container.
         */
        static set<T, K extends keyof T>(values: ServiceMetadata<T, K>[]): Container;

        /**
         * Sets a value for the given type or service name in the container.
         */
        static set(identifierOrServiceMetadata: ServiceIdentifier | ServiceMetadata<any, any> | (ServiceMetadata<any, any>[]), value?: any): Container {
            this.globalInstance.set(identifierOrServiceMetadata as any, value);
            return this;
        }

        /**
         * Removes services with a given service identifiers (tokens or types).
         */
        static remove(...ids: ServiceIdentifier[]): Container {
            this.globalInstance.remove(...ids);
            return this;
        }

        /**
         * Completely resets the container by removing all previously registered services and handlers from it.
         */
        static reset(containerId?: any): Container {
            if (containerId) {
                const instance = this.instances.find(instance => instance.id === containerId);
                if (instance) {
                    instance.reset();
                    this.instances.splice(this.instances.indexOf(instance), 1);
                }

            } else {
                this.globalInstance.reset();
                this.instances.forEach(instance => instance.reset());
            }
            return this;
        }

        /**
         * Registers a new handler.
         */
        static registerHandler(handler: Handler): Container {
            this.handlers.push(handler);
            return this;
        }

        /**
         * Helper method that imports given services.
         */
        static import(services: Function[]): Container {
            return this;
        }

    }