Skip to content

Instantly share code, notes, and snippets.

@krishnathota
Last active June 22, 2021 02:02
Show Gist options
  • Select an option

  • Save krishnathota/1477bb8d4cb72da31ee509d14dba5f56 to your computer and use it in GitHub Desktop.

Select an option

Save krishnathota/1477bb8d4cb72da31ee509d14dba5f56 to your computer and use it in GitHub Desktop.

Revisions

  1. krishnathota renamed this gist Jun 22, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. krishnathota renamed this gist Jun 22, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. krishnathota created this gist Jun 22, 2021.
    23 changes: 23 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import { Injectable, OnDestroy } from '@angular/core';
    import { SubscriptionLike } from 'rxjs';
    import { SubSink } from 'subsink';
    /**
    * @description Keeps hold of your subscriptions and unsubscribes them when component is destroyed.
    * This uses 'SubSink' library to achieve this.
    * @usage Extend your component from this 'MyComponent extends Unsubscribe'. Call super() in your component's constructor.
    * @important Please keep a note that if you happen to implement OnDestroy in your component,
    * you have to explicitly call super.ngOnDestroy() to invoke un-subscription for the component.
    * @param sub; Subscription list. Assign your subscriptions in your component to 'sub'
    */
    @Injectable()
    export abstract class Unsubscribe implements OnDestroy {
    _subSink = new SubSink();

    set sub(subscription: SubscriptionLike) {
    this._subSink.sink = subscription;
    }

    ngOnDestroy(): void {
    this._subSink.unsubscribe();
    }
    }