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OAAO,CAAC,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CACzC,CAAC;IACH,CAAC;IAEO,qDAAkB,GAA1B,UAA2B,MAAc,EAAE,IAAY,EAAE,UAA+B;QACtF,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;YACf,MAAM,CAAC,UAAU,CAAC;QACpB,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,wBAAwB,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;IAC7D,CAAC;IACH,+BAAC;AAAD,CAAC,AArLD,IAqLC;AArLY,4DAAwB;AAuLxB,QAAA,gBAAgB,GAAG,IAAI,wBAAwB,EAAE,CAAC","sourcesContent":["import isFunction = require('lodash/isFunction');\n\nimport {\n InstanceChainMap,\n LodashDecorator,\n InstanceChainContext\n} from './common';\nimport { DecoratorConfig } from './DecoratorConfig';\nimport {\n copyMetadata,\n bind,\n isMethodOrPropertyDecoratorArgs,\n isPrototypeAccess\n} from '../utils';\n\nexport type GenericDecorator = (...args: any[]) => LodashDecorator;\n\nexport class InternalDecoratorFactory {\n createDecorator(config: DecoratorConfig): GenericDecorator {\n const { applicator, optionalParams } = config;\n\n return (...args: any[]): LodashDecorator => {\n let params = args;\n\n const decorator = (target: Object, name: string, _descriptor?: PropertyDescriptor): PropertyDescriptor => {\n const descriptor = this._resolveDescriptor(target, name, _descriptor);\n const { value, get, set } = descriptor;\n\n // If this decorator is being applied after an instance decorator we simply ignore it\n // as we can't apply it correctly.\n if (!InstanceChainMap.has([ target, name ])) {\n if (isFunction(value)) {\n descriptor.value = copyMetadata(applicator.apply({ config, target, value, args: params }), value);\n } else if (isFunction(get) && config.getter) {\n descriptor.get = copyMetadata(applicator.apply({ config, target, value: get, args: params }), get);\n } else if (isFunction(set) && config.setter) {\n descriptor.set = copyMetadata(applicator.apply({ config, target, value: set, args: params }), set);\n }\n }\n\n return descriptor;\n };\n\n if (optionalParams && isMethodOrPropertyDecoratorArgs(...args)) {\n params = [];\n\n return decorator(args[0], args[1], args[2]) as any;\n }\n\n return decorator;\n };\n }\n\n createInstanceDecorator(config: DecoratorConfig): GenericDecorator {\n const { applicator, bound, optionalParams } = config;\n\n return (...args: any[]): LodashDecorator => {\n let params = args;\n const decorator = (target: Object, name: string, _descriptor?: PropertyDescriptor): PropertyDescriptor => {\n const descriptor = this._resolveDescriptor(target, name, _descriptor);\n const { value, writable, enumerable, configurable, get, set } = descriptor;\n const isFirstInstance = !InstanceChainMap.has([ target, name ]);\n const chainData = InstanceChainMap.get([ target, name ]) || { fns: [], properties: [] };\n const isGetter = isFirstInstance && isFunction(get);\n const isSetter = isFirstInstance && isFunction(set);\n const isMethod = isFirstInstance && isFunction(value);\n const isProperty = isFirstInstance && !isGetter && !isSetter && !isMethod;\n const baseValue = isGetter ? get : isMethod ? value : undefined;\n\n chainData.properties.push(name);\n chainData.fns.push((fn: Function, instance: any, context: InstanceChainContext) => {\n if (!this._isApplicable(context, config)) {\n return fn;\n }\n\n if (bound) {\n fn = bind(fn, instance);\n }\n\n return copyMetadata(\n applicator.apply({ args: params, target, instance, value: fn, config }),\n fn\n );\n });\n\n InstanceChainMap.set([ target, name ], chainData);\n\n if (!isFirstInstance) {\n return descriptor;\n }\n\n chainData.isSetter = isSetter;\n chainData.isGetter = isGetter;\n chainData.isMethod = isMethod;\n chainData.isProperty = isProperty;\n\n const applyChain = (fn: any, context: InstanceChainContext, instance: any) => {\n return chainData.fns.reduce((result: Function, next: Function) => next(result, instance, context), fn);\n };\n\n const applyDecorator = (instance: any) => {\n let getter = get || undefined;\n let setter = set || undefined;\n\n if (isGetter || isSetter) {\n // If we have a getter apply the decorators to the getter and assign it to the instance.\n if (isGetter) {\n getter = applyChain(get, { value: get, getter: true }, instance);\n }\n\n if (isSetter) {\n setter = applyChain(set, { value: set, setter: true }, instance);\n }\n\n Object.defineProperty(instance, name, {\n enumerable,\n configurable,\n get: getter,\n set: setter\n });\n } else if (isMethod || isProperty) {\n const newFn = isMethod\n ? applyChain(value, { value, method: true }, instance)\n : applyChain(value, { value, property: true }, instance);\n\n Object.defineProperty(instance, name, {\n writable,\n enumerable,\n configurable,\n value: newFn\n });\n }\n }\n\n if (isMethod || isProperty) {\n delete descriptor.value;\n delete descriptor.writable;\n }\n\n descriptor.get = function() {\n // Check for direct access on the prototype.\n // MyClass.prototype.fn <-- This should not apply the decorator.\n if (isPrototypeAccess(this, target)) {\n return baseValue;\n }\n\n applyDecorator(this);\n\n const descriptor = Object.getOwnPropertyDescriptor(this, name)!;\n\n if (descriptor.get) {\n return descriptor.get.call(this);\n }\n\n return descriptor.value;\n };\n\n descriptor.set = function(value) {\n applyDecorator(this);\n\n const descriptor = Object.getOwnPropertyDescriptor(this, name)!;\n\n if (descriptor.set) {\n descriptor.set.call(this, value);\n } else if (isProperty || isMethod) {\n this[name] = value;\n }\n };\n\n return descriptor;\n };\n\n if (optionalParams && isMethodOrPropertyDecoratorArgs(...args)) {\n params = [];\n\n return decorator(args[0], args[1], args[2]) as any;\n }\n\n return decorator;\n };\n }\n\n private _isApplicable(context: InstanceChainContext, config: DecoratorConfig): boolean {\n return !Boolean(\n context.getter && !config.getter\n || context.setter && !config.setter\n || context.method && !config.method\n || context.property && !config.property\n );\n }\n\n private _resolveDescriptor(target: Object, name: string, descriptor?: PropertyDescriptor): PropertyDescriptor {\n if (descriptor) {\n return descriptor;\n }\n\n return Object.getOwnPropertyDescriptor(target, name) || {};\n }\n}\n\nexport const DecoratorFactory = new InternalDecoratorFactory();\n"]}
\No newline at end of file