npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-unsub

v1.0.6

Published

Class level decorator which will automatically unsubscribe all the subscriptions

Downloads

54

Readme

ngx-unsub

Simple package to automatically handle all the subscription at the class level

Supports Angular v12 to v17

Installation

npm i ngx-unsub

Usage

  1. Import the decorator in the component
import { NgxUnsubscribe } from 'ngx-unsubscribe';
  1. Add the decorator to the top of the class
@NgxUnsubscribe()
export class HomeComponent implements OnInit {

}

Thats it !!!

You heard it right.

Yes the package will take care of every method which has return type Subscrption

For example:

@NgxUnsubscribe()
export class HomeComponent implements OnInit {
  constructor(private apiService: ApiService) {}

  count = 0;
  count2 = 0;

  ngOnInit() {
    this.getCount();
    this.getCountTwice();
    this.getPosts();
    this.getComments();
  }

  getCount(): Subscription {
    return interval(2000).subscribe(() => {
      this.count += 1;
      console.log(this.count);
    });
  }

  getCountTwice(): Subscription {
    return interval(500).subscribe(() => {
      this.count2 += 2;
      console.log(this.count2);
    });
  }

  getPosts(): Subscription {
    return this.apiService.getPosts().subscribe((res) => {
      console.log(res);
    });
  }

  getComments(): Subscription {
    return this.apiService.getComments().subscribe((res) => {
      console.log(res);
    });
  }
}

Here actually see can the methods getCount(), getCountTwice(), getPosts() and getComments() have return type Subscription. But none of those subscriptions are unsubscribed in ngOnDestroy().

Here the decorator comes in help which automatically decide the subscription based on return type and unsubscribes it.

Note: function should have return keyword and return type should be Subscription

Advantages of using this package

  1. No need of manual unsubscription
  2. No need of adding ngOnDestroy method
  3. No need of adding decorator to each method
  4. No need of declaring Subscription or Subscription array variables
  5. Light weight package