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,EAAE,UAAU,CAAC,CAAC;YAC9C,CAAC;QACF,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IACZ,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,eAAe,MAAM,CAAC","sourcesContent":["import WeakMap from '@dojo/shim/WeakMap';\nimport { WidgetBase } from './../WidgetBase';\nimport { handleDecorator } from './handleDecorator';\nimport { beforeProperties } from './beforeProperties';\nimport { InjectorItem, RegistryLabel } from './../interfaces';\n\n/**\n * Map of instances against registered injectors.\n */\nconst registeredInjectorsMap: WeakMap<WidgetBase, InjectorItem[]> = new WeakMap();\n\n/**\n * Defines the contract requires for the get properties function\n * used to map the injected properties.\n */\nexport interface GetProperties<T = any> {\n\t(payload: any, properties: T): T;\n}\n\n/**\n * Defines the inject configuration required for use of the `inject` decorator\n */\nexport interface InjectConfig {\n\t/**\n\t * The label of the registry injector\n\t */\n\tname: RegistryLabel;\n\n\t/**\n\t * Function that returns propertues to inject using the passed properties\n\t * and the injected payload.\n\t */\n\tgetProperties: GetProperties;\n}\n\n/**\n * Decorator retrieves an injector from an available registry using the name and\n * calls the `getProperties` function with the payload from the injector\n * and current properties with the the injected properties returned.\n *\n * @param InjectConfig the inject configuration\n */\nexport function inject({ name, getProperties }: InjectConfig) {\n\treturn handleDecorator((target, propertyKey) => {\n\t\tbeforeProperties(function(this: WidgetBase & { own: Function }, properties: any) {\n\t\t\tconst injectorItem = this.registry.getInjector(name);\n\t\t\tif (injectorItem) {\n\t\t\t\tconst { injector, invalidator } = injectorItem;\n\t\t\t\tconst registeredInjectors = registeredInjectorsMap.get(this) || [];\n\t\t\t\tif (registeredInjectors.length === 0) {\n\t\t\t\t\tregisteredInjectorsMap.set(this, registeredInjectors);\n\t\t\t\t}\n\t\t\t\tif (registeredInjectors.indexOf(injectorItem) === -1) {\n\t\t\t\t\tthis.own(\n\t\t\t\t\t\tinvalidator.on('invalidate', () => {\n\t\t\t\t\t\t\tthis.invalidate();\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\t\t\t\t\tregisteredInjectors.push(injectorItem);\n\t\t\t\t}\n\t\t\t\treturn getProperties(injector(), properties);\n\t\t\t}\n\t\t})(target);\n\t});\n}\n\nexport default inject;\n"]}