Skip to content

Commit 76e4097

Browse files
authored
Fixed: getResolved yields before state is updated (#10)
* fix editor config * add timer hack to the facade * reduce to 1 ms
1 parent b9dfe4c commit 76e4097

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

ngrx-http-tracking/.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ indent_style = space
77
indent_size = 2
88
insert_final_newline = true
99
trim_trailing_whitespace = true
10+
end_of_line = crlf
1011

1112
[*.ts]
1213
quote_type = single

ngrx-http-tracking/projects/ngrx-http-tracking/src/lib/+state/http-tracking.facade.ts

+23-17
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as HttpTrackingActions from './http-tracking.actions';
22
import * as HttpTrackingSelectors from './http-tracking.selectors';
33
import { Action, Store } from '@ngrx/store';
44
import { HttpTrackingEntity } from '../model/http-tracking-entity';
5-
import { debounceTime, filter, map, take } from 'rxjs/operators';
5+
import { debounceTime, filter, map, switchMap, take } from 'rxjs/operators';
66
import { Injectable } from '@angular/core';
7-
import { forkJoin, Observable } from 'rxjs';
7+
import { forkJoin, Observable, timer } from 'rxjs';
88
import { isError } from '../function/is-error';
99
import { mapActionTypeToId } from '../function/map-action-typ-to-id';
1010
import { TrackingAction } from '../function/http-tracking-actions.factory';
@@ -77,21 +77,27 @@ export class HttpTrackingFacade {
7777
}
7878

7979
public getResolved<T1, T2>(action: TrackingAction<T1, T2>): Observable<HttpTrackingResult<T1, T2>> {
80-
return this.getTracking(action).pipe(
81-
filter(tracking => !!tracking),
82-
map(tracking => (<HttpTrackingEntity>tracking).httpStatus),
83-
filter(httpStatus => httpStatus === LoadingState.LOADED || isError(httpStatus)),
84-
take(1),
85-
map(httpStatus => {
86-
const retVal = <HttpTrackingResult<T1, T2>>{
87-
action,
88-
success: httpStatus === LoadingState.LOADED,
89-
};
90-
if (isError(httpStatus)) {
91-
retVal.error = httpStatus;
92-
}
93-
return retVal;
94-
})
80+
// this timer is here to prevent an issue with retrieving the state before
81+
// the reducer is updated on a second call to the same tracked action
82+
return timer(1).pipe(
83+
switchMap(() =>
84+
this.getTracking(action).pipe(
85+
filter(tracking => !!tracking),
86+
map(tracking => (<HttpTrackingEntity>tracking).httpStatus),
87+
filter(httpStatus => httpStatus === LoadingState.LOADED || isError(httpStatus)),
88+
take(1),
89+
map(httpStatus => {
90+
const retVal = <HttpTrackingResult<T1, T2>>{
91+
action,
92+
success: httpStatus === LoadingState.LOADED,
93+
};
94+
if (isError(httpStatus)) {
95+
retVal.error = httpStatus;
96+
}
97+
return retVal;
98+
})
99+
)
100+
)
95101
);
96102
}
97103

0 commit comments

Comments
 (0)