RxJS
Introduction
RxJS is a widely used in the industry library to work with async code use reactive paradigm.
From the official website:
RxJS is a library for composing asynchronous and event-based programs by using observable sequences.
RxJS is used extensively at Snap and supported by Valdi. RxJS library is available under valdi_rxjs module.
Linters
To further enforce some of the best practices, Valdi enables the following RxJS linter rules (click the URL to see the up to date linter description):
rxjs/no-sharereplay
rxjs/no-async-subscribe
rxjs/no-ignored-observable
rxjs/no-ignored-subscription
To avoid memory leaks and unexpected crashes all created subscriptions must be unsubscribed using the unsubscribe call or using one of the helpers below.
There are two ways to ease to manage subscriptions in Valdi:
- Use Subscription#add method and invoke Subscription#unsubscribe inside
onDestroy - Use Component#registerDisposable method which would invoke
unsubscribemethod for you duringonDestroy
Use Component#registerDisposable
Use Component#registerDisposable to automatically call unsubscribe/dispose during onDestroy for created subscriptions/disposables if your class is a child of Component:
import { Component } from 'valdi_core/src/Component';
import { Observable } from 'valdi_rxjs/src/Observable';
export class MyComponent extends Component {
private myObservable: Observable<boolean> = new Observable<boolean>();
onCreate() {
const sub = this.myObservable.subscribe(data => {
// Handle data
});
this.registerDisposable(sub);
}
}
Use Subscription#add
Use Subscription#add to keep track of created subscription and manually control when unsubscribe method is called:
import { Subscription } from 'valdi_rxjs/src/Subscription';
export class MyComponent {
private subscription: Subscription = new Subscription();
constructor() {
const sub = this.myObservable.subscribe(data => {
// Handle data
});
this.subscription.add(sub);
}
cleanup() {
this.subscription.unsubscribe();
}
}