私は観測上の最適化問題を持っている:rxjs/angular4観測可能な最適化
私はユーザー名とパスワードを受け入れる2つのコントロールを持っています。パスワードフィールドの値が変更されると(デバウンスを使用して)、クエリ結果を観測可能な状態に戻すサービスメソッドを呼び出し、その値でモデルを埋めます。
返されたオブザーバブルは、サービスエラーの場合に例外をスローします。しかし、私はそのような場合に空の配列でモデルを塗りつぶしたいだけです。
私の元の実装:
this.passwordControl.valueChanges
.debounceTime(500)
.filter(_ => this.usernameControl.valid && !this.usernameControl.pristine)
.map((password) => new RequestAuthentication(this.usernameControl.value, password))
.mergeMap(auth => this.orgService.getOrganizations(auth))
.catch(_ => {
return Observable.of([]);
})
.subscribe(orgs => {
this.organizations = orgs;
});
欠点がキャッチ機能が実行されたとき、私はストリームを完了し、私は変化にもう聞くことができない有限の観測可能を返すということです。
私のソリューションは、次のような巣の観測にある:
this.passwordControl.valueChanges
.debounceTime(500)
.filter(_ => this.usernameControl.valid && !this.usernameControl.pristine)
.map((password) => new RequestAuthentication(this.usernameControl.value, password))
.subscribe(auth => {
this.orgService.getOrganizations(auth)
.catch(_ => Observable.of([]))
.subscribe(orgs => {
this.organizations = orgs;
});
});
しかし、これは非常に反応思えません...どのように私はこれらのネストされた観測を避けるため、クリーンなコードを維持するために行う必要がありますか?
なぜあなたは 'orgService.getOrganizations'代わりで例外をキャッチしませんか?この方法では、最初のスニペットフローを破ることはありません – bviale
rxjsの再試行/キャッシュされた値のフォールバックを試してみました。 https://github.com/ipassynk/angular2-learning/blob/master/src/app/http-rxjs/http-rxjs.component.ts –