Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
src
.gitignore
README.md
package-lock.json
package.json
public_api.ts
tsconfig.dev.json
tsconfig.json

README.md

NPM

Demo

Auto Subscriptions

Typescript library for automagically handling subscribe() / unsubscribe() of Observable properties of classes.

Installation

npm install auto-subscriptions

Usage

Add @AutoSubscriptions to the class and @AutoSubscription to the class observable properties, for which you want automatic subscription handling:

import { AutoSubscriptions } from 'auto-subscriptions';

@AutoSubscriptions({
  init: 'init',
  destroy: 'destroy'
})
export class MyClass {
  @AutoSubscription
  myObs$: Observable<boolean> = of(true);

  @AutoSubscription
  myObsD$: Observable<boolean> = of(false);
  
  init() {
  }
  destroy() {
  }
}

Another way:

import { AutoSubscriptions } from 'auto-subscriptions';
import { Observable, of } from 'rxjs';

@AutoSubscriptions({
  init: MyClassB.prototype.init,
  destroy: MyClassB.prototype.destroy
})
export class MyClassB {
  @AutoSubscription
  myObs$: Observable<boolean> = of(true);

  @AutoSubscription
  myObsD$: Observable<boolean> = of(false);
  
  init() {
  }
  destroy() {
  }
}

when init is called subscribe() will be invoked for all @AutoSubscription observable properies,

when destroy is called unsubscribe() will be invoked for all @AutoSubscription observable properies,

For example:

  const myClass = new MyClass();
  myClass.init() /* subscribe() is invoked for all @AutoSubscription observable properies, */
  
  // code ...
  
  myClass.destroy() /* unsubscribe() is invoked for all @AutoSubscription observable properies */
  

Angular components:

import { Component } from '@angular/core';
import { AutoSubscriptions } from 'auto-subscriptions';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
@AutoSubscriptions({
  init: 'ngOnInit',
  destroy: 'ngOnDestroy'
})
export class AppComponent {
  result: Object;
  
  @AutoSubscription
  httpOnInit$: Observable<Object> = this.http.get('HTTP_URL').pipe(
    tap((result: Object) => this.result = result)
  );
  
  constructor(protected http: HttpClient) {
  }
  
}