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P,CAAC;IAEL,wBAAC;AAAD,CAjWA,AAiWC,IAAA;AAjWY,8CAAiB","file":"ContainerInstance.js","sourcesContent":["import {ServiceMetadata} from \"./types/ServiceMetadata\";\nimport {ObjectType} from \"./types/ObjectType\";\nimport {Token} from \"./Token\";\nimport {ServiceIdentifier} from \"./types/ServiceIdentifier\";\nimport {ServiceNotFoundError} from \"./error/ServiceNotFoundError\";\nimport {MissingProvidedServiceTypeError} from \"./error/MissingProvidedServiceTypeError\";\nimport {Container} from \"./Container\";\n\n/**\n * TypeDI can have multiple containers.\n * One container is ContainerInstance.\n */\nexport class ContainerInstance {\n\n // -------------------------------------------------------------------------\n // Public Properties\n // -------------------------------------------------------------------------\n\n /**\n * Container instance id.\n */\n id: any;\n\n // -------------------------------------------------------------------------\n // Private Properties\n // -------------------------------------------------------------------------\n\n /**\n * All registered services.\n */\n private services: ServiceMetadata<any, any>[] = [];\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(id: any) {\n this.id = id;\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Checks if the service with given name or type is registered service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n has<T>(type: ObjectType<T>): boolean;\n\n /**\n * Checks if the service with given name or type is registered service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n has<T>(id: string): boolean;\n\n /**\n * Checks if the service with given name or type is registered service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n has<T>(id: Token<T>): boolean;\n\n /**\n * Checks if the service with given name or type is registered service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n has<T>(identifier: ServiceIdentifier): boolean {\n return !!this.findService(identifier);\n }\n\n /**\n * Retrieves the service with given name or type from the service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n get<T>(type: ObjectType<T>): T;\n\n /**\n * Retrieves the service with given name or type from the service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n get<T>(id: string): T;\n\n /**\n * Retrieves the service with given name or type from the service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n get<T>(id: Token<T>): T;\n\n /**\n * Retrieves the service with given name or type from the service container.\n * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.\n */\n get<T>(identifier: ServiceIdentifier): T {\n\n const globalContainer = Container.of(undefined);\n let service = globalContainer.findService(identifier);\n let scopedService = this.findService(identifier);\n\n if (service && service.global === true)\n return this.getServiceValue(identifier, service);\n\n if (scopedService)\n return this.getServiceValue(identifier, scopedService);\n\n if (service && this !== globalContainer) {\n const clonedService = Object.assign({}, service);\n clonedService.value = undefined;\n const value = this.getServiceValue(identifier, clonedService);\n this.set(identifier, value);\n return value;\n }\n\n return this.getServiceValue(identifier, service);\n }\n\n /**\n * Gets all instances registered in the container of the given service identifier.\n * Used when service defined with multiple: true flag.\n */\n getMany<T>(id: string): T[];\n\n /**\n * Gets all instances registered in the container of the given service identifier.\n * Used when service defined with multiple: true flag.\n */\n getMany<T>(id: Token<T>): T[];\n\n /**\n * Gets all instances registered in the container of the given service identifier.\n * Used when service defined with multiple: true flag.\n */\n getMany<T>(id: string|Token<T>): T[] {\n return this.filterServices(id).map(service => this.getServiceValue(id, service));\n }\n\n /**\n * Sets a value for the given type or service name in the container.\n */\n set<T, K extends keyof T>(service: ServiceMetadata<T, K>): this;\n\n /**\n * Sets a value for the given type or service name in the container.\n */\n set(type: Function, value: any): this;\n\n /**\n * Sets a value for the given type or service name in the container.\n */\n set(name: string, value: any): this;\n\n /**\n * Sets a value for the given type or service name in the container.\n */\n set(token: Token<any>, value: any): this;\n\n /**\n * Sets a value for the given type or service name in the container.\n */\n set(token: ServiceIdentifier, value: any): this;\n\n /**\n * Sets a value for the given type or service name in the container.\n */\n set<T, K extends keyof T>(values: ServiceMetadata<T, K>[]): this;\n\n /**\n * Sets a value for the given type or service name in the container.\n */\n set(identifierOrServiceMetadata: ServiceIdentifier|ServiceMetadata<any, any>|(ServiceMetadata<any, any>[]), value?: any): this {\n if (identifierOrServiceMetadata instanceof Array) {\n identifierOrServiceMetadata.forEach((v: any) => this.set(v));\n return this;\n }\n if (typeof identifierOrServiceMetadata === \"string\" || identifierOrServiceMetadata instanceof Token) {\n return this.set({ id: identifierOrServiceMetadata, value: value });\n }\n if (identifierOrServiceMetadata instanceof Function) {\n return this.set({ type: identifierOrServiceMetadata, id: identifierOrServiceMetadata, value: value });\n }\n\n // const newService: ServiceMetadata<any, any> = arguments.length === 1 && typeof identifierOrServiceMetadata === \"object\" && !(identifierOrServiceMetadata instanceof Token) ? identifierOrServiceMetadata : undefined;\n const newService: ServiceMetadata<any, any> = identifierOrServiceMetadata;\n const service = this.findService(newService.id);\n if (service && service.multiple !== true) {\n Object.assign(service, newService);\n } else {\n this.services.push(newService);\n }\n\n return this;\n }\n\n /**\n * Removes services with a given service identifiers (tokens or types).\n */\n remove(...ids: ServiceIdentifier[]): this {\n ids.forEach(id => {\n this.filterServices(id).forEach(service => {\n this.services.splice(this.services.indexOf(service), 1);\n });\n });\n return this;\n }\n\n /**\n * Completely resets the container by removing all previously registered services from it.\n */\n reset(): this {\n this.services = [];\n return this;\n }\n\n // -------------------------------------------------------------------------\n // Private Methods\n // -------------------------------------------------------------------------\n\n /**\n * Filters registered service in the with a given service identifier.\n */\n private filterServices(identifier: ServiceIdentifier): ServiceMetadata<any, any>[] {\n return this.services.filter(service => {\n if (service.id)\n return service.id === identifier;\n\n if (service.type && identifier instanceof Function)\n return service.type === identifier || identifier.prototype instanceof service.type;\n\n return false;\n });\n }\n\n /**\n * Finds registered service in the with a given service identifier.\n */\n private findService(identifier: ServiceIdentifier): ServiceMetadata<any, any>|undefined {\n return this.services.find(service => {\n if (service.id)\n return service.id === identifier;\n\n if (service.type && identifier instanceof Function)\n return service.type === identifier; // todo: not sure why it was here || identifier.prototype instanceof service.type;\n\n return false;\n });\n }\n\n /**\n * Gets service value.\n */\n private getServiceValue(identifier: ServiceIdentifier, service: ServiceMetadata<any, any>|undefined): any {\n\n // find if instance of this object already initialized in the container and return it if it is\n if (service && service.value !== null && service.value !== undefined)\n return service.value;\n\n // if named service was requested and its instance was not found plus there is not type to know what to initialize,\n // this means service was not pre-registered and we throw an exception\n if ((!service || !service.type) &&\n (!service || !service.factory) &&\n (typeof identifier === \"string\" || identifier instanceof Token))\n throw new ServiceNotFoundError(identifier);\n\n // at this point we either have type in service registered, either identifier is a target type\n let type = undefined;\n if (service && service.type) {\n type = service.type;\n\n } else if (service && service.id instanceof Function) {\n type = service.id;\n\n } else if (identifier instanceof Function) {\n type = identifier;\n }\n\n // if service was not found then create a new one and register it\n if (!service) {\n if (!type)\n throw new MissingProvidedServiceTypeError(identifier);\n\n service = { type: type };\n this.services.push(service);\n }\n\n // setup constructor parameters for a newly initialized service\n const paramTypes = type && Reflect && (Reflect as any).getMetadata ? (Reflect as any).getMetadata(\"design:paramtypes\", type) : undefined;\n let params: any[] = paramTypes ? this.initializeParams(type, paramTypes) : [];\n\n // if factory is set then use it to create service instance\n let value: any;\n if (service.factory) {\n\n // filter out non-service parameters from created service constructor\n // non-service parameters can be, lets say Car(name: string, isNew: boolean, engine: Engine)\n // where name and isNew are non-service parameters and engine is a service parameter\n params = params.filter(param => param !== undefined);\n\n if (service.factory instanceof Array) {\n // use special [Type, \"create\"] syntax to allow factory services\n // in this case Type instance will be obtained from Container and its method \"create\" will be called\n value = (this.get(service.factory[0]) as any)[service.factory[1]](...params);\n\n } else { // regular factory function\n value = service.factory(...params);\n }\n\n } else { // otherwise simply create a new object instance\n if (!type)\n throw new MissingProvidedServiceTypeError(identifier);\n\n params.unshift(null);\n\n // \"extra feature\" - always pass container instance as the last argument to the service function\n // this allows us to support javascript where we don't have decorators and emitted metadata about dependencies\n // need to be injected, and user can use provided container to get instances he needs\n params.push(this);\n\n value = new (type.bind.apply(type, params))();\n }\n\n if (service && !service.transient && value)\n service.value = value;\n\n if (type)\n this.applyPropertyHandlers(type, value);\n\n return value;\n }\n\n /**\n * Initializes all parameter types for a given target service class.\n */\n private initializeParams(type: Function, paramTypes: any[]): any[] {\n return paramTypes.map((paramType, index) => {\n const paramHandler = Container.handlers.find(handler => handler.object === type && handler.index === index);\n if (paramHandler)\n return paramHandler.value(this);\n\n if (paramType && paramType.name && !this.isTypePrimitive(paramType.name)) {\n return this.get(paramType);\n }\n\n return undefined;\n });\n }\n\n /**\n * Checks if given type is primitive (e.g. string, boolean, number, object).\n */\n private isTypePrimitive(param: string): boolean {\n return [\"string\", \"boolean\", \"number\", \"object\"].indexOf(param.toLowerCase()) !== -1;\n }\n\n /**\n * Applies all registered handlers on a given target class.\n */\n private applyPropertyHandlers(target: Function, instance: { [key: string]: any }) {\n Container.handlers.forEach(handler => {\n if (typeof handler.index === \"number\") return;\n if (handler.object.constructor !== target && !(target.prototype instanceof handler.object.constructor))\n return;\n\n instance[handler.propertyName] = handler.value(this);\n });\n }\n\n}\n"],"sourceRoot":"."}
\No newline at end of file