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ACD,OAAO,gBAAgB,CAAC,GAAG,CAAI,SAAS,CAAC,CAAC;AAC5C,CAAC","sourcesContent":["/**\n * Container options.\n */\nexport interface UseContainerOptions {\n /**\n * If set to true, then default container will be used in the case if given container haven't returned anything.\n */\n fallback?: boolean;\n\n /**\n * If set to true, then default container will be used in the case if given container thrown an exception.\n */\n fallbackOnErrors?: boolean;\n}\n\n/**\n * Container to be used by this library for inversion control. If container was not implicitly set then by default\n * container simply creates a new instance of the given class.\n */\nconst defaultContainer: { get<T>(someClass: { new (...args: any[]): T } | Function): T } = new (class {\n private instances: { type: Function; object: any }[] = [];\n get<T>(someClass: { new (...args: any[]): T }): T {\n let instance = this.instances.find(instance => instance.type === someClass);\n if (!instance) {\n instance = { type: someClass, object: new someClass() };\n this.instances.push(instance);\n }\n\n return instance.object;\n }\n})();\n\nlet userContainer: { get<T>(someClass: { new (...args: any[]): T } | Function): T };\nlet userContainerOptions: UseContainerOptions;\n\n/**\n * Sets container to be used by this library.\n */\nexport function useContainer(iocContainer: { get(someClass: any): any }, options?: UseContainerOptions): void {\n userContainer = iocContainer;\n userContainerOptions = options;\n}\n\n/**\n * Gets the IOC container used by this library.\n */\nexport function getFromContainer<T>(someClass: { new (...args: any[]): T } | Function): T {\n if (userContainer) {\n try {\n const instance = userContainer.get(someClass);\n if (instance) return instance;\n\n if (!userContainerOptions || !userContainerOptions.fallback) return instance;\n } catch (error) {\n if (!userContainerOptions || !userContainerOptions.fallbackOnErrors) throw error;\n }\n }\n return defaultContainer.get<T>(someClass);\n}\n"]} |