Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ngneat/until-destroy

Package Overview
Dependencies
Maintainers
4
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngneat/until-destroy

RxJS operator that unsubscribes when Angular component is destroyed

  • 9.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
160K
decreased by-19.89%
Maintainers
4
Weekly downloads
 
Created

What is @ngneat/until-destroy?

@ngneat/until-destroy is an Angular utility that helps manage the lifecycle of subscriptions and other resources in Angular components and services. It automatically unsubscribes from observables when the component or service is destroyed, preventing memory leaks and reducing boilerplate code.

What are @ngneat/until-destroy's main functionalities?

Automatic Unsubscription

This feature allows automatic unsubscription from observables when the component is destroyed. The `untilDestroyed` operator is used to bind the observable to the component's lifecycle.

import { Component } from '@angular/core';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { interval } from 'rxjs';

@UntilDestroy()
@Component({
  selector: 'app-example',
  template: '<p>Example works!</p>'
})
export class ExampleComponent {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe(val => console.log(val));
  }
}

Service Support

This feature extends the automatic unsubscription functionality to services. The `untilDestroyed` operator can be used in services to manage subscriptions and other resources.

import { Injectable } from '@angular/core';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { interval } from 'rxjs';

@UntilDestroy()
@Injectable({
  providedIn: 'root'
})
export class ExampleService {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe(val => console.log(val));
  }
}

Directive Support

This feature allows the use of `untilDestroyed` in directives, providing the same automatic unsubscription functionality as in components and services.

import { Directive, OnDestroy } from '@angular/core';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { interval } from 'rxjs';

@UntilDestroy()
@Directive({
  selector: '[appExample]'
})
export class ExampleDirective implements OnDestroy {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe(val => console.log(val));
  }

  ngOnDestroy() {
    // Custom cleanup logic if needed
  }
}

Other packages similar to @ngneat/until-destroy

Keywords

FAQs

Package last updated on 20 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc