Skip to content

Commit

Permalink
fix: ensure viewRef existence and detectChanges
Browse files Browse the repository at this point in the history
docs: use knobs to improve story
  • Loading branch information
JounQin committed Jul 21, 2019
1 parent ea459ff commit bfa92cd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 53 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.3.0",
"version": "0.3.1",
"description": "🎉 Angular + RxJS + TypeScript = 🔥",
"repository": "[email protected]:rx-ts/ngrx.git",
"author": "JounQin <[email protected]>",
Expand Down
8 changes: 5 additions & 3 deletions src/directives/async.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,22 @@ export class AsyncDirective<T, P, E = HttpErrorResponse>
withLatestFrom(this.retryTimes$),
)
.subscribe(([[context, fetcher, params], retryTimes]) => {
this.disposeSub()

Object.assign(this.context, {
loading: true,
error: null,
})

this.disposeSub()

this.sub = fetcher
.call(context, params)
.pipe(
retry(retryTimes),
finalize(() => {
this.context.loading = false
this.viewRef!.markForCheck()
if (this.viewRef) {
this.viewRef.detectChanges()
}
}),
)
.subscribe(
Expand Down
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.3.0",
"version": "0.3.1",
"description": "🎉 Angular + RxJS + TypeScript = 🔥",
"repository": "[email protected]:rx-ts/ngrx.git",
"author": "JounQin <[email protected]>",
Expand Down
66 changes: 18 additions & 48 deletions stories/directives/directives.stories.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
import { CommonModule } from '@angular/common'
import { HttpClient, HttpClientModule } from '@angular/common/http'
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
} from '@angular/core'
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'
import { RxDirectivesModule } from '@rxts/ngrx'
import { number } from '@storybook/addon-knobs'
import { storiesOf } from '@storybook/angular'
import { debounce } from 'lodash'
import { EMPTY, Subject, interval } from 'rxjs'
import { map, pairwise, take } from 'rxjs/operators'

@Component({
selector: 'rx-async-directive-demo',
template: `
<label>
Todo Id:
<input
placeholder="Please enter a todo id, 1-200"
type="number"
[ngModel]="todoId"
(ngModelChange)="onTodoIdChange($event)"
/>
</label>
<label>
Retry Times:
<input
placeholder="Please enter the retry times on error"
type="number"
[ngModel]="retryTimes"
(ngModelChange)="onRetryTimesChange($event)"
/>
</label>
<button (click)="refetch$$.next()">Refetch (Outside rxAsync)</button>
<div
*rxAsync="
Expand All @@ -57,33 +36,15 @@ import { map, pairwise, take } from 'rxjs/operators'
class AsyncDirectiveComponent {
context = this

@Input()
todoId = 1
retryTimes?: number

refetch$$ = new Subject<void>()

onTodoIdChange = debounce(function(
this: AsyncDirectiveComponent,
todoId: number,
) {
this.todoId = todoId
this.cdr.markForCheck()
},
500)
@Input()
retryTimes = 0

onRetryTimesChange = debounce(function(
this: AsyncDirectiveComponent,
retryTimes: number,
) {
if (typeof retryTimes !== 'number') {
return
}
this.retryTimes = retryTimes
this.cdr.markForCheck()
},
500)
refetch$$ = new Subject<void>()

constructor(private cdr: ChangeDetectorRef, private http: HttpClient) {}
constructor(private http: HttpClient) {}

fetchTodo(todoId: string) {
return typeof todoId === 'number'
Expand All @@ -100,7 +61,16 @@ const moduleMetadata = {
storiesOf('Directives', module)
.add('Async Directive', () => ({
moduleMetadata,
component: AsyncDirectiveComponent,
template: /* HTML */ `
<rx-async-directive-demo
[todoId]="todoId"
[retryTimes]="retryTimes"
></rx-async-directive-demo>
`,
props: {
todoId: number('todoId', 1),
retryTimes: number('retryTimes', 0),
},
}))
.add(
'Var Directive',
Expand Down

0 comments on commit bfa92cd

Please sign in to comment.