Skip to content

Commit 2084356

Browse files
drumoniiyggg
authored andcommitted
fix(nbIsGranted): prevent subscription memory leak (#2087)
1 parent 0b22c40 commit 2084356

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/framework/security/directives/is-granted.directive.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Directive, Input, OnDestroy, TemplateRef, ViewContainerRef } from '@angular/core';
2-
import { takeWhile } from 'rxjs/operators';
2+
import { takeUntil } from 'rxjs/operators';
3+
import { Subject } from 'rxjs';
34

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

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

9-
private alive = true;
10+
private destroy$ = new Subject<void>();
11+
1012
private hasView = false;
1113

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

1921
this.accessChecker.isGranted(permission, resource)
2022
.pipe(
21-
takeWhile(() => this.alive),
23+
takeUntil(this.destroy$),
2224
)
2325
.subscribe((can: boolean) => {
2426
if (can && !this.hasView) {
@@ -32,6 +34,7 @@ export class NbIsGrantedDirective implements OnDestroy {
3234
}
3335

3436
ngOnDestroy() {
35-
this.alive = false;
37+
this.destroy$.next();
38+
this.destroy$.complete();
3639
}
3740
}

0 commit comments

Comments
 (0)