11.0.0 • Published 3 years ago

@mszewcz/safe-subscribe v11.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

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 {
    }
}
11.0.0

3 years ago

1.0.21

4 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago