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

fix(nbIsGranted): prevent subscription memory leak #2087

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/framework/security/directives/is-granted.directive.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Directive, Input, OnDestroy, TemplateRef, ViewContainerRef } from '@angular/core';
import { takeWhile } from 'rxjs/operators';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';

import { NbAccessChecker } from '../services/access-checker.service';

@Directive({ selector: '[nbIsGranted]'})
export class NbIsGrantedDirective implements OnDestroy {

private alive = true;
private destroy$ = new Subject<void>();

private hasView = false;

constructor(private templateRef: TemplateRef<any>,
Expand All @@ -18,7 +20,7 @@ export class NbIsGrantedDirective implements OnDestroy {

this.accessChecker.isGranted(permission, resource)
.pipe(
takeWhile(() => this.alive),
takeUntil(this.destroy$),
)
.subscribe((can: boolean) => {
if (can && !this.hasView) {
Expand All @@ -32,6 +34,7 @@ export class NbIsGrantedDirective implements OnDestroy {
}

ngOnDestroy() {
this.alive = false;
this.destroy$.next();
this.destroy$.complete();
}
}