Skip to content

Instantly share code, notes, and snippets.

@glutengo
Created August 29, 2018 21:28
Show Gist options
  • Select an option

  • Save glutengo/619367a89e4bd2d87fffeff1f7094b9f to your computer and use it in GitHub Desktop.

Select an option

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)
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