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
- Host: GitHub
- URL: https://github.com/marcj/angular-unsub
- Owner: marcj
- Created: 2018-12-14T15:48:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-14T16:05:35.000Z (over 7 years ago)
- Last Synced: 2025-01-02T14:44:24.111Z (over 1 year ago)
- Topics: angular
- Language: TypeScript
- Size: 1.95 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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) => {
});
}
}
```