执行后取消了Angular 5 / Angular-redux Epics调用

时间:2018-12-06 10:53:12

标签: angular rxjs angular5 angular-httpclient angular-redux

我使用Angular 5和Angular-redux进行了设置,以拥有包含史诗和动作的Redux存储。

当我想分派fetchNodes史诗时,会发生我的问题。 如果通过身份验证,它将毫无问题地获取fetchNodes,因为这是第一次调用。但是,当我先执行登录史诗/动作,然后执行fetchNodes操作/ epic时,该调用会立即被取消。当我尝试进行新呼叫时,流(ActionsObservable流或HttpClient流)可能仍处于打开状态。

This post建议我使用mergeMap进行httpClient调用,但第一次仍无法使用。

fetchNodes = (action$: ActionsObservable<any>) => {
    const fetchNodesFulfilled = payload => ({ type: NodesActions.FETCH_NODES_SUCCESS, payload });
    return action$.pipe(
        ofType(NodesActions.FETCH_NODES),
        mergeMap(
            ({payload}) => {
                let headers = new HttpHeaders({
                    'Authorization': 'basic ' + this.auth.user.getValue().token,
                });
                return this.http.get<Nodes>(`${ENV.apiUrl}/nodes`, { headers: headers })
                    .map(payload => fetchNodesFulfilled(payload.nodes[0]))
                    .catch((error) => {
                        return Observable.of({
                            type: NodesActions.FETCH_NODES_FAILED
                        })
                    });
            })
        ).catch((error) => {
            return Observable.of({
            type: NodesActions.FETCH_NODES_FAILED
        }) 
    })
}

login = (action$: ActionsObservable<any>) => {
    return action$.pipe(
        ofType(SessionActions.LOGIN_USER),
        mergeMap(
            ({payload}) => {
            let headers = new HttpHeaders({
                'Authorization': 'basic ' + payload
            });
            return this.http.post<AuthUser>(`${ENV.apiUrl}/authentication`, {}, { headers: headers })
                 .map((succes) => {
                    return Observable.of({
                        type: SessionActions.LOGIN_SUCCESS,
                        payload: succes
                    })
                });
                .catch((error) => {
                    return Observable.of({
                        type: SessionActions.LOGIN_FAILED,
                        payload: {}
                    })
                });
            }
        )).catch((error) => {
            return Observable.of({
                type: SessionActions.LOGIN_FAILED,
                payload: {}
            })
        });
}

Here's an image of the canceled call

0 个答案:

没有答案