JSPM

@orchestrator/ngx-until-destroyed

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q70322F
  • License MIT

RxJS operator that unsubscribes from observable on Angular component destruction

Package Exports

  • @orchestrator/ngx-until-destroyed

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@orchestrator/ngx-until-destroyed) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

🤓 Angular - Unsubscribe For Pros 💪

Build Status Coverage Npm Npm Downloads Licence semantic-release

Declarative way to unsubscribe from observables when the component destroyed

Installation

$ npm install @orchestrator/ngx-until-destroyed --save

Usage

import { untilDestroyed } from '@orchestrator/ngx-until-destroyed';

@Component({
  selector: 'app-inbox',
  templateUrl: './inbox.component.html',
})
export class InboxComponent implements OnInit, OnDestroy {
  ngOnInit() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe(val => console.log(val));
  }

  // This method must be present, even if empty.
  ngOnDestroy() {
    // To protect you, we'll throw an error if it doesn't exist.
  }
}

Use with decorator

import { WithUntilDestroyed } from '@orchestrator/ngx-until-destroyed';

@Component({...})
class MyComponent implements OnDestroy {
  @WithUntilDestroyed()
  stream$ = interval(1000); // You can safely subscribe to this everywhere

  // This method must be present, even if empty
  ngOnDestroy() {}
}

Use with any class

import { untilDestroyed } from '@orchestrator/ngx-until-destroyed';

export class Widget {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this, 'destroy'))
      .subscribe(console.log);
  }

  // The name needs to be the same as the decorator parameter
  destroy() {}
}