Skip to content

Commit

Permalink
fix(nbIsGranted): prevent subscription memory leak (#2087)
Browse files Browse the repository at this point in the history
  • Loading branch information
drumonii authored and yggg committed Nov 20, 2019
1 parent 0b22c40 commit 2084356
Showing 1 changed file with 7 additions and 4 deletions.
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();
}
}

0 comments on commit 2084356

Please sign in to comment.