eslint-rxjs-destroy
v1.0.6
Published
ESLint rule to ensure that RxJS subscriptions are properly destroyed to prevent memory leaks.
Maintainers
Readme
ESLint Plugin
eslint-rxjs-destroy is an ESLint plugin designed to help developers ensure that RxJS subscriptions are properly destroyed, preventing memory leaks in Angular and other RxJS-based applications.
Features
- Enforces cleanup of RxJS subscriptions.
- Helps maintain best practices for managing subscriptions in Angular components and services.
- Prevents common memory leak issues caused by unhandled subscriptions.
Installation
To install the plugin, run:
npm install --save-dev eslint-rxjs-destroyPeer Dependencies
This plugin requires ESLint as a peer dependency. Ensure you have a compatible version of ESLint installed:
npm install --save-dev eslintSupported ESLint versions: ^7.0.0 || ^8.0.0 || ^9.0.0.
Usage
Add eslint-rxjs-destroy to your ESLint configuration file:
.eslintrc.json
{
"plugins": ["rxjs-destroy"],
"rules": {
"rxjs-destroy/ensure-destroy": "error"
}
}Example Rule
The ensure-destroy rule ensures that any RxJS subscription is properly cleaned up, typically by calling unsubscribe() or using Angular's OnDestroy lifecycle hook.
Valid Example
import { Component, OnDestroy } from "@angular/core";
import { Subscription } from "rxjs";
@Component({
selector: "app-example",
template: "<p>Example</p>",
})
export class ExampleComponent implements OnDestroy {
private subscription: Subscription;
constructor() {
this.subscription = someObservable.subscribe();
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
}Invalid Example
import { Component } from "@angular/core";
import { Subscription } from "rxjs";
@Component({
selector: "app-example",
template: "<p>Example</p>",
})
export class ExampleComponent {
private subscription: Subscription;
constructor() {
this.subscription = someObservable.subscribe();
}
}Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License.
