Last active
September 23, 2015 21:06
-
-
Save jimthedev/53ae4b69dc8cd1a20245 to your computer and use it in GitHub Desktop.
Lifecycle callback tracking / logging for Angular 2 (with console color by component)
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
/** | |
* Have your component extend this class temporarily when you | |
* need lifecycle callback tracking / debugging. See second | |
* file for how to use this class in your component. | |
*/ | |
class LifecycleCallbackTracker { | |
staticCallCounts: Object = {}; | |
textColor: string; | |
constructor(textColor?: string) { | |
this.staticCallCounts = this.constructor['_callCounts']; | |
this.textColor=textColor; | |
} | |
incrementAndEchoCallCounts(lifecycleCallbackName: string) { | |
let count = this.staticCallCounts[lifecycleCallbackName]; | |
if(count) { | |
count++; | |
} else { | |
count=1; | |
} | |
this.staticCallCounts[lifecycleCallbackName] = count; | |
console.log('%c' + this.className() + '->' + lifecycleCallbackName + '() call count: ' + count, 'color:'+this.textColor); | |
} | |
className(): string { | |
return this.constructor.toString().match(/\w+/g)[1]; | |
} | |
} |
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
/** | |
* This component will output lifecycle event counts to the console as this component is loaded. | |
* You must manually add references into lifecycle callbacks which you're interested in. | |
* | |
* To use this tracker properly, you should: | |
* | |
* 1. Have a static object called _callCounts on your component to hold counts. | |
* | |
* 2. Use this.incrementAndEchoCallCounts(callbackName:string) inside of any lifecycle callbacks you wish to track. | |
* Make sure to pass in a name for the lifecycle callback you are tracking. Avoid 'constructor' as it will | |
* cause errors. | |
* | |
* These steps are shown in the example component below. | |
* | |
* To easily toggle tracking/logging while leaving your callback functions untouched, simply add a noop implementation | |
* of this.incrementAndEchoCallCounts(callbackName:string) then comment/uncomment it out as needed. | |
*/ | |
@Component({ | |
selector: 'my-component' | |
}) | |
@View({ | |
directives: [ CORE_DIRECTIVES, FORM_DIRECTIVES], | |
template: ` | |
<my-sub-component> | |
</my-sub-component> | |
` | |
}) | |
export class MyComponent extends LifecycleCallbackTracker { | |
static callCounts: Object = {}; | |
constructor() { | |
super('#c00'); | |
this.incrementAndEchoCallCounts('_constructor'); // Use the counter to track the constructor | |
} | |
onInit() { | |
this.incrementAndEchoCallCounts('onInit'); // Use the counter, this time for tracking onInit | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment