@mszewcz/safe-subscribe

RxJS safeSubscribe operator holding & automatically unsubscribing all subscriptions inside a component

Usage no npm install needed!

<script type="module">
  import mszewczSafeSubscribe from 'https://cdn.skypack.dev/@mszewcz/safe-subscribe';
</script>

README

safeSubscribe

Automatically unsubscribes from RxJS observables in Angular components/services.

Installation

npm i @mszewcz/safe-subscribe

Usage

Use it as you would use RxJS subscribe operator, just pass component instance (this) as first parameter.

Be sure to implement ngOnDestroy method in your component/service.

safeSubscribe(classRef: Object, next?: Function, error?: Function, complete?: Function): Subscription

Arguments:

  • classRef - A reference to the object that is holding the observable.
  • next - A handler for each delivered value.
  • error - A handler for an error notification.
  • complete - A handler for the execution-complete notification.

Returns:

  • A Subscription object.

Example component:

import { Component, OnDestroy, OnInit } from '@angular/core';

import { interval } from 'rxjs';
import '@mszewcz/safe-subscribe';

@Component({
    selector: 'app-test',
})
export class TestComponent implements OnDestroy, OnInit {
    
    public ngOnInit(): void {    
        interval(1000).safeSubscribe(
            this,
            val => console.log(val)
        );
    }

    /**
     * At least a noop ngOnDestroy is required for safeSubscribe to work
     */    
    public ngOnDestroy(): void {
    }
}