Skip to content

Commit

Permalink
fix(core): fixes memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
thekiba committed Oct 10, 2018
1 parent 145b047 commit 0e5c431
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
22 changes: 18 additions & 4 deletions projects/platform/src/lib/directives/http.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Directive, Input, TemplateRef, ViewContainerRef, EmbeddedViewRef, OnChanges, SimpleChanges } from '@angular/core';
import { Directive, Input, TemplateRef, ViewContainerRef, EmbeddedViewRef, OnChanges, SimpleChanges, OnDestroy } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { of } from 'rxjs';
import { of, Subscription } from 'rxjs';
import { catchError } from 'rxjs/operators';

enum HttpStrategies {
Expand Down Expand Up @@ -69,7 +69,7 @@ const HTTP_CONFIG: HttpStrategy[] = [
];

@Directive({ selector: '[http]' })
export class HttpDirective implements OnChanges {
export class HttpDirective implements OnChanges, OnDestroy {

@Input() httpDelete: string;
@Input() httpGet: string;
Expand All @@ -92,6 +92,8 @@ export class HttpDirective implements OnChanges {
private viewRef: EmbeddedViewRef<HttpContext> =
this.viewContainerRef.createEmbeddedView(this.templateRef, this.context);

private subscription: Subscription;

constructor(
private http: HttpClient,
private templateRef: TemplateRef<HttpContext>,
Expand All @@ -105,6 +107,10 @@ export class HttpDirective implements OnChanges {
}
}

ngOnDestroy() {
this.dispose();
}

private findStrategy(changes: SimpleChanges): HttpStrategy {
return HTTP_CONFIG.find((strategy) => {
return strategy.changes.some(field => !!changes[field])
Expand All @@ -119,7 +125,8 @@ export class HttpDirective implements OnChanges {
}

private request(method, ...params) {
this.http[method](...params)
this.dispose();
this.subscription = this.http[method](...params)
.pipe(catchError((e) => {
console.error(e);
return of(null);
Expand All @@ -129,4 +136,11 @@ export class HttpDirective implements OnChanges {
this.viewRef.markForCheck();
});
}

private dispose() {
if (this.subscription && !this.subscription.closed) {
this.subscription.unsubscribe();
this.subscription = null;
}
}
}
34 changes: 26 additions & 8 deletions projects/platform/src/lib/directives/timeout.directive.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { Directive, Input, ViewContainerRef, TemplateRef } from '@angular/core';
import { Directive, Input, ViewContainerRef, TemplateRef, OnDestroy } from '@angular/core';

@Directive({ selector: '[timeout]' })
export class TimeoutDirective {
export class TimeoutDirective implements OnDestroy {

private timeoutId: number;

constructor(
private templateRef: TemplateRef<null>,
private viewContainerRef: ViewContainerRef
) {
}
) {}

@Input() set timeout(milliseconds: number) {
this.viewContainerRef.clear();
this.dispose();
this.create(milliseconds);
}

setTimeout(() => {
this.viewContainerRef.createEmbeddedView(this.templateRef);
}, milliseconds);
ngOnDestroy() {
this.dispose();
}

private create(milliseconds: number) {
this.timeoutId = setTimeout(() => {
if (this.viewContainerRef) {
this.viewContainerRef.createEmbeddedView(this.templateRef);
}
}, milliseconds) as any as number;
}

private dispose() {
this.viewContainerRef.clear();
if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
}
}
}
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--The content below is only a placeholder and can be replaced.-->
<!--<router-outlet></router-outlet>-->
<router-outlet></router-outlet>

<ng-container *cookies="let cookie set 'ngxf' value 'Best Of The Best' path '/' domain: 'localhost' expires '01 Oct 2020'">
{{ cookie }}
Expand Down

0 comments on commit 0e5c431

Please # to comment.