11.0.0 • Published 4 years ago
@mszewcz/safe-subscribe v11.0.0
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
4 years ago
1.0.21
5 years ago
1.0.20
6 years ago
1.0.19
6 years ago
1.0.18
6 years ago
1.0.17
7 years ago
1.0.16
7 years ago
1.0.15
7 years ago
1.0.14
7 years ago
1.0.13
7 years ago
1.0.12
7 years ago
1.0.11
7 years ago
1.0.10
7 years ago
1.0.9
7 years ago
1.0.8
7 years ago
1.0.7
7 years ago
1.0.6
7 years ago
1.0.5
7 years ago
1.0.4
7 years ago
1.0.3
7 years ago
1.0.2
7 years ago
1.0.1
7 years ago
1.0.0
7 years ago