Skip to content

Feature Request: ngOnDestroy for Observable Subscriptions #9

Open
@danielzen

Description

@danielzen

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

Activity

macjohnny

macjohnny commented on Sep 26, 2020

@macjohnny
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

danielzen commented on Oct 7, 2020

@danielzen
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Feature Request: ngOnDestroy for Observable Subscriptions · Issue #9 · angular-extensions/lint-rules