An open API service indexing awesome lists of open source software.

https://github.com/marcj/angular-unsub

Unsubscribes automatically your RXJS subscriptions when component is ngOnDestroy'ed
https://github.com/marcj/angular-unsub

angular

Last synced: 6 months ago
JSON representation

Unsubscribes automatically your RXJS subscriptions when component is ngOnDestroy'ed

Awesome Lists containing this project

README

          

# Angular-unsub

Automatically unsubscribes your subscriptions of your components
when the component is destroyed.

## Example

## `unsubscribe()` decorator

A decorator for class property, which automatically calls unsubscribe() on that property value
when the component is destroyed (when ngOnDestroy is called).

When you re-assign the property value, the old one will automatically be unsubscribed as well.

```typescript
@Component({
template: 'Hi'
})
class MyComponent {
@Input() data: Observable;

@unsubscribe()
private sub: Subscription;

constructor() {
this.sub = this.data.subscribe((next) => {

});

//old one will be unsubscribed as well
this.sub = this.data.subscribe((next) => {

});
}
}
```

## `Subscriptions` class

Allows to collect multiple subscriptions in one collection to simplify code.

```typescript
@Component({
template: 'Hi'
})
class MyComponent {
@Input() data: Observable;
@Input() data2: Observable;

@unsubscribe()
private subs = new Subscriptions();

constructor() {
this.sub.add = this.data.subscribe((next) => {

});

this.sub.add = this.data2.subscribe((next) => {

});
}
}
```