Skip to content

Commit

Permalink
fix: dispose last subscription automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 21, 2019
1 parent a389b12 commit 50bc4a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rxts/ngrx",
"version": "0.2.0",
"version": "0.2.1",
"description": "🎉 Angular + RxJS + TypeScript = 🔥",
"repository": "[email protected]:rx-ts/ngrx.git",
"author": "JounQin <[email protected]>",
Expand Down
16 changes: 14 additions & 2 deletions src/directives/async.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ViewContainerRef,
ViewRef,
} from '@angular/core'
import { Observable, Subject, combineLatest } from 'rxjs'
import { Observable, Subject, Subscription, combineLatest } from 'rxjs'
import { startWith, takeUntil } from 'rxjs/operators'

import { Callback, Nullable } from '../types/public-api'
Expand Down Expand Up @@ -49,6 +49,7 @@ export class AsyncDirective<T, P, E = HttpErrorResponse>
} as IAsyncDirectiveContext<T, E>

private viewRef: Nullable<ViewRef>
private sub: Nullable<Subscription>

constructor(
private templateRef: TemplateRef<any>,
Expand All @@ -74,7 +75,9 @@ export class AsyncDirective<T, P, E = HttpErrorResponse>
error: null,
})

fetcher.call(context, params).subscribe(
this.disposeSub()

this.sub = fetcher.call(context, params).subscribe(
data => (this.context.$implicit = data),
error => (this.context.error = error),
() => {
Expand All @@ -95,6 +98,8 @@ export class AsyncDirective<T, P, E = HttpErrorResponse>
}

ngOnDestroy() {
this.disposeSub()

this.destroy$$.next()
this.destroy$$.complete()

Expand All @@ -103,4 +108,11 @@ export class AsyncDirective<T, P, E = HttpErrorResponse>
this.viewRef = null
}
}

disposeSub() {
if (this.sub) {
this.sub.unsubscribe()
this.sub = null
}
}
}
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rxts/ngrx",
"version": "0.2.0",
"version": "0.2.1",
"description": "🎉 Angular + RxJS + TypeScript = 🔥",
"repository": "[email protected]:rx-ts/ngrx.git",
"author": "JounQin <[email protected]>",
Expand Down

0 comments on commit 50bc4a1

Please sign in to comment.