2016-08-19 11 views
1

Angular2を使用して、@ ngrx/store &/effects。RxJS-角度2 - 条件付きストリーム

サービスの条件付き結果を処理するストリームを作成する方法を試しています...これは、ユーザーがログインしているかどうかを確認するために、アプリケーションが最初に実行されたときに動作します。 LocalStorageに格納されます。

これはこれを処理するための完全に間違った方法かもしれませんので、助言をいただければ幸いです。私は効果

@Effect() check$ = this.updates$ 
.whenAction(AuthActions.AUTHENTICATE_CHECK) 
.map(update => this.authService.checkAuth()) 
// now what? 
// returns an observable of false, or a Auth object if LS info found 
// needs to return 
// this.authActions.authenticateCheckResult(res) if ok and 
// this.authActions.authenticateCheckError() if not ok 

を持っており、authServicecheckAuth方法は次のようにある瞬間

...

checkAuth():Observable { 
this._user = <AuthAPI>this.ls.getObject(AuthService.LS_AUTH_KEY); 

if(this._user && this._user.accessToken) { 
    return Observable.of(this._user); 
} 
else { 
    return Observable.of(false); 
} 
} 

更新

私が持っていますこれをやっているのは...

@Effect() check$ = this.updates$ 
.whenAction(AuthActions.AUTHENTICATE_CHECK) 
.map(update => this.authService.checkAuth()) 
.map(result => { 
    if(!result) { 
    return this.authActions.authenticateCheckError(false); 
    } else { 
    return this.authActions.authenticateCheckSuccess(result) 
    } 
}); 

//

checkAuth():AuthAPI { 
this._user = <AuthAPI>this.ls.getObject(AuthService.LS_AUTH_KEY); 

if(this._user && this._user.accessToken) { 
    return this._user; 
} 
else { 
    return null; 
} 

}

これは、これを行うのに有効な方法であるか良い方法はありますか?

+1

が私には正常に見えるようにまた、あなたの呼び出しをチェーンすることができます。 –

+0

よろしくお願いします。 – markstewie

答えて

0

checkAuth():Observable<AuthAPI> { 
    return Rx.Observable.of(<AuthAPI>this.ls.getObject(AuthService.LS_AUTH_KEY)) 
         .filter(user => user) 
         .filter(user.accessToken) 
         .defaultIfEmpty(null) 
関連する問題