-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(*): Rewrite deprecation decorator for properties and methods (#…
- Loading branch information
Borislav Kulov
committed
Dec 18, 2018
1 parent
a2e731b
commit 25b5cfb
Showing
3 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 54 additions & 6 deletions
60
projects/igniteui-angular/src/lib/core/deprecateDecorators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,65 @@ | ||
import { isDevMode } from '@angular/core'; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export function DeprecateMethod(message: string): MethodDecorator { | ||
return (constructor: any) => { | ||
console.warn(constructor.constructor.name + ': ' + message); | ||
}; | ||
var isMessageShown = false; | ||
|
||
return function (target: any, key: string, descriptor: PropertyDescriptor) { | ||
if (descriptor && descriptor.value) { | ||
const originalMethod = descriptor.value; | ||
|
||
descriptor.value = function () { | ||
if (!isMessageShown && isDevMode()) { | ||
const targetName = typeof target === "function" ? target.name : target.constructor.name; | ||
isMessageShown = true; | ||
|
||
console.warn(`${targetName}.${key}: ${message}`); | ||
} | ||
|
||
return originalMethod.apply(this, arguments); | ||
}; | ||
|
||
return descriptor; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export function DeprecateProperty(message: string): PropertyDecorator { | ||
return (constructor: any) => { | ||
console.warn(constructor.constructor.name + ': ' + message); | ||
}; | ||
return function(target: any, key: string) { | ||
var isMessageShown = false; | ||
|
||
// 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, { | ||
set(value) { | ||
this[newKey] = value; | ||
}, | ||
get() { | ||
if (!isMessageShown && isDevMode()) { | ||
isMessageShown = true; | ||
console.warn(`${target.constructor.name}.${key}: ${message}`); | ||
} | ||
|
||
return this[newKey]; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
function generateUniqueKey(target: any, key: string): string { | ||
let newKey = '_' + key; | ||
while (target.hasOwnProperty(newKey)) { | ||
newKey = '_' + newKey; | ||
} | ||
|
||
return newKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters