Skip to content

Commit

Permalink
chore(*): Invoke original getter/setter (#2915)
Browse files Browse the repository at this point in the history
Invoke original getter/setter for deprecated properties.
  • Loading branch information
Borislav Kulov committed Dec 18, 2018
1 parent 808603d commit 21c71cf
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions projects/igniteui-angular/src/lib/core/deprecateDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ export function DeprecateMethod(message: string): MethodDecorator {
const originalMethod = descriptor.value;

descriptor.value = function () {
if (!isMessageShown && isDevMode()) {
const targetName = typeof target === 'function' ? target.name : target.constructor.name;
isMessageShown = true;
const targetName = typeof target === 'function' ? target.name : target.constructor.name;
isMessageShown = showMessage(`${targetName}.${key}: ${message}`, isMessageShown);

console.warn(`${targetName}.${key}: ${message}`);
}

return originalMethod.apply(this, arguments);
return originalMethod.call(this, arguments);
};

return descriptor;
Expand All @@ -36,14 +32,30 @@ export function DeprecateProperty(message: string): PropertyDecorator {
// use backing field to set/get the value of the property to ensure there won't be infinite recursive calls
const newKey = generateUniqueKey(target, key);

Object.defineProperty(target, key, {
let getter, setter;
const orignalDescriptor = Object.getOwnPropertyDescriptor(target, key);
if (orignalDescriptor) {
getter = orignalDescriptor.get;
setter = orignalDescriptor.set;

delete target[key];
}

return Object.defineProperty(target, key, {
set(value) {
this[newKey] = value;
isMessageShown = showMessage(`${target.constructor.name}.${key}: ${message}`, isMessageShown);

if (setter) {
setter.call(this, value);
} else {
this[newKey] = value;
}
},
get() {
if (!isMessageShown && isDevMode()) {
isMessageShown = true;
console.warn(`${target.constructor.name}.${key}: ${message}`);
isMessageShown = showMessage(`${target.constructor.name}.${key}: ${message}`, isMessageShown);

if (getter) {
return getter.call(this);
}

return this[newKey];
Expand All @@ -63,3 +75,14 @@ function generateUniqueKey(target: any, key: string): string {

return newKey;
}

/**
* @hidden
*/
function showMessage(message: string, isMessageShown: boolean): boolean {
if (!isMessageShown && isDevMode()) {
console.warn(message);
}

return true;
}

0 comments on commit 21c71cf

Please sign in to comment.