2017-04-04 71 views
0

我有一個我想與RxJS解決以下問題(角4)RxJS重試獲取數據登錄後 - 如果登錄嘗試一次

  • 如果對數據的請求沒有登錄失敗(403禁止)
  • 如果請求失敗,返回403我想在登錄時重試數據請求
  • 登錄時我想在用戶訪問需要數據的視圖時請求數據一次

我有以下流

const isLoggedIn$ = store.select('currentuser').filter(user => user.id) 
const loadActions$ = storeActions$.filter(action => action.type == 'LOAD_DATA_REQUEST') 

我有一些代碼,但它不履行上述

loadActions$.take(1).subscribe(() => { http.get('/api/data') ... }) 

作品的所有條件,當我登錄,我允許訪問數據,但是當我失敗在未登錄米

回答

0

第一:你不應該做一個GET調用一個訂閱內,他這樣說, 你可以使用retryWhen此:

const isLoggedIn$ = store.select('currentuser').filter(user => user.id) 
const loadActions$ = storeActions$.ofType('LOAD_DATA_REQUEST'); 

loadActions$ 
    .take(1) // not sure if you need this, I just copied it from your code 
    .switchMap(() => { 
     return http.get('/api/data') 
      .retryWhen(error => isLoggedIn$.take(1)); // optional: use the "error" parameter to check if the error is 403 
    })