Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: ngOnDestroy for Observable Subscriptions #9

Open
danielzen opened this issue Sep 25, 2020 · 2 comments
Open

Feature Request: ngOnDestroy for Observable Subscriptions #9

danielzen opened this issue Sep 25, 2020 · 2 comments

Comments

@danielzen
Copy link

Some might need a reminder by tslint, to remind us in the console that our components or directives should have a ngOnDestroy method when it detects none.
We can add a custom rule to tslint to warn us in the console during linting and building if it finds no ngOnDestroy hook in our components:

Item #6 contains the code:
https://blog.bitsrc.io/6-ways-to-unsubscribe-from-observables-in-angular-ab912819a78f

@macjohnny
Copy link
Member

@danielzen thanks for your request.

whenever an observable is subscribed to within a component, the angular-rxjs-takeuntil-before-subscribe rule ensures that there is an ngOnDestroy method

// check the ngOnDestroyMethod
const destroySubjectNamesUsedList = Object.keys(destroySubjectNamesUsed);
destroySubjectNamesUsedList.forEach(destroySubjectNameUsed => {
// look for ngOnDestroy in class and in all parent classes
const classesToCheck = [
componentClassDeclaration,
...this.findParentClasses(program, componentClassDeclaration)
];
const ngOnDestroyFailuresList = classesToCheck.map(classDeclaration => this.checkNgOnDestroy(
sourceFile,
classDeclaration,
destroySubjectNameUsed
));
// if there is no correct implementation of ngOnDestroy in any of the classes to be checked
if (ngOnDestroyFailuresList.length > 0 && !ngOnDestroyFailuresList.find(failures => failures.length === 0)) {
failures.push(...ngOnDestroyFailuresList[0]);
}
});
return failures;
}

/**
* Checks whether the class implements an ngOnDestroy method and invokes .next() on the destroy subjects
*/
private checkNgOnDestroy(
sourceFile: ts.SourceFile,
classDeclaration: ts.ClassDeclaration,
destroySubjectNameUsed: string
): Lint.RuleFailure[] {

how is your feature request different from that?

@danielzen
Copy link
Author

Not sure why it didn't work for me, but I will double check. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants