Skip to content

AniaSniadek/auto-destroy-decorator

Repository files navigation

@AutoDestroy decorator 🔥

This is the source code for the @AutoDestroy property decorator. The main purpose of this decorator is to automatically destroy subscriptions in Angular components. This only works on a Subject<void> variable.

If you want to test it check it out on StackBlitz ⚡️.

Example of usage

Create property decorator @AutoDestroy:

export function AutoDestroy(component: any, key: string | symbol): void {
  const originalOnDestroy = component.ngOnDestroy;

  component.ngOnDestroy = function () {
    if (originalOnDestroy) {
      originalOnDestroy.call(this);
    }
    this[key].next();
    this[key].complete();
  };
}

Use it in component like this (no need to add ngOnDestroy()):

@Component(...)
export class AppComponent {
  @AutoDestroy destroy$: Subject<void> = new Subject<void>();
}

About

Property decorator for automatically unsubscribe

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published