Created
August 29, 2018 21:28
-
-
Save glutengo/619367a89e4bd2d87fffeff1f7094b9f to your computer and use it in GitHub Desktop.
Pausable component which completely turns off angular change detection (should components should have ChangeDetectionStrategy.OnPush)
This file contains hidden or 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
| export class PausableComponent { | |
| private detectChanges; | |
| private getCdr() { | |
| const cdrKey = Object.keys(this).find(key => this[key] &&typeof this[key].detectChanges === 'function'); | |
| return cdrKey && this[cdrKey]; | |
| } | |
| pause() { | |
| const cdr = this.getCdr(); | |
| cdr.detach(); | |
| this.detectChanges = Object.getPrototypeOf(cdr).detectChanges; | |
| cdr.detectChanges = () => { | |
| return; | |
| }; | |
| } | |
| continue() { | |
| const cdr = this.getCdr(); | |
| cdr.reattach(); | |
| cdr.detectChanges = this.detectChanges; | |
| cdr.detectChanges(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment