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gBAAgB,CAAC,GAAG,CAAI,SAAS,CAAC,CAAC;AAC9C,CAAC;AAhBD,4CAgBC","file":"container.js","sourcesContent":["/**\n * Container options.\n */\nexport interface UseContainerOptions {\n\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\nexport type ContainedType<T> = { new (...args: any[]): T } | Function;\n\nexport interface ContainerInterface {\n get<T>(someClass: ContainedType<T>): T;\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: ContainerInterface = new (class implements ContainerInterface {\n private instances: { type: Function, object: any }[] = [];\n\n get<T>(someClass: ContainedType<T>): T {\n let instance = this.instances.find(i => i.type === someClass);\n if (!instance) {\n instance = { type: someClass, object: new (someClass as new() => T)()};\n this.instances.push(instance);\n }\n\n return instance.object;\n }\n})();\n\nlet userContainer: ContainerInterface;\nlet userContainerOptions: UseContainerOptions|undefined;\n\n/**\n * Sets container to be used by this library.\n */\nexport function useContainer(iocContainer: ContainerInterface, options?: UseContainerOptions) {\n userContainer = iocContainer;\n userContainerOptions = options;\n}\n\n/**\n * Gets the IOC container used by this library.\n */\nexport function getFromContainer<T>(someClass: ContainedType<T>): T {\n if (userContainer) {\n try {\n const instance = userContainer.get(someClass);\n if (instance)\n return instance;\n\n if (!userContainerOptions || !userContainerOptions.fallback)\n return instance;\n\n } catch (error) {\n if (!userContainerOptions || !userContainerOptions.fallbackOnErrors)\n throw error;\n }\n }\n return defaultContainer.get<T>(someClass);\n}"],"sourceRoot":"."}
\No newline at end of file