Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@ngneat/until-destroy
Advanced tools
RxJS operator that unsubscribes when Angular component is destroyed
@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.
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
}
}
ngx-take-until-destroy is another Angular utility for managing subscriptions. It provides a similar functionality to @ngneat/until-destroy by using a `takeUntilDestroy` operator to handle unsubscriptions. However, @ngneat/until-destroy offers a more modern and streamlined API.
take-until-destroy is a lightweight library that helps manage subscriptions in Angular components. It uses a `takeUntil` operator to unsubscribe from observables. While it provides similar functionality, @ngneat/until-destroy offers a more comprehensive and integrated solution.
A neat way to unsubscribe from observables when the component destroyed
npm install @ngneat/until-destroy
# Or if you use yarn
yarn add @ngneat/until-destroy
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
@UntilDestroy()
@Component({})
export class InboxComponent {
ngOnInit() {
interval(1000)
.pipe(untilDestroyed(this))
.subscribe();
}
}
You can set the checkProperties
option to true
if you want to unsubscribe from subscriptions properties automatically:
@UntilDestroy({ checkProperties: true })
@Component({})
export class HomeComponent {
// We'll dispose it on destroy
subscription = fromEvent(document, 'mousemove').subscribe();
}
You can set the arrayName
property if you want to unsubscribe from each subscription in the specified array.
@UntilDestroy({ arrayName: 'subscriptions' })
@Component({})
export class HomeComponent {
subscriptions = [
fromEvent(document, 'click').subscribe(),
fromEvent(document, 'mousemove').subscribe()
];
// You can still use the opertator
ngOnInit() {
interval(1000).pipe(untilDestroyed(this));
}
}
@UntilDestroy()
@Injectable()
export class InboxService {
constructor() {
interval(1000)
.pipe(untilDestroyed(this))
.subscribe();
}
}
@Component({
providers: [InboxService]
})
export class InboxComponent {
constructor(inboxService: InboxService) {}
}
All options, described above, are also applicable to providers.
npm install ngx-take-until-destroy
# Or if you use yarn
yarn add ngx-take-until-destroy
import { untilDestroyed } from 'ngx-take-until-destroy';
@Component({})
export class InboxComponent implements OnDestroy {
ngOnInit() {
interval(1000)
.pipe(untilDestroyed(this))
.subscribe(val => console.log(val));
}
// This method must be present, even if empty.
ngOnDestroy() {
// To protect you, we'll throw an error if it doesn't exist.
}
}
import { untilDestroyed } from 'ngx-take-until-destroy';
export class Widget {
constructor() {
interval(1000)
.pipe(untilDestroyed(this, 'destroy'))
.subscribe(console.log);
}
// The name needs to be the same as the second parameter
destroy() {}
}
To make it easier for you to migrate, we've built a script that will update the imports path, and add the decorator for you. You need to run it manually on your project.
Thanks goes to these wonderful people (emoji key):
Netanel Basal 💻 📖 🤔 | Artur Androsovych 💻 💡 🤔 🚇 | Krzysztof Karol 🖋 | Alex Malkevich 💻 | Khaled Shaaban 💻 | kmathy 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
RxJS operator that unsubscribes when Angular component is destroyed
The npm package @ngneat/until-destroy receives a total of 155,242 weekly downloads. As such, @ngneat/until-destroy popularity was classified as popular.
We found that @ngneat/until-destroy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.