条件付きで2つのソースから読み取る観測可能なストリームを構築しようとしています。ユーザーの選択に基づくファイルストリーム、または現在のセッションのインメモリストリーム。観測対象にユーザーの選択に基づいてストリームを条件付きで返すようにするにはどうすればよいですか?
私は、ユーザーが以下のいずれかを選択することができる場所ドロップダウンを持っている:ユーザーが新しい値を選択したときに
Current //in-memory stream contains entries (error, warning, trace, debug) as they happen for the current session
Error //error.log file entries
Warning //warning.log
Trace //trace.log
Debug //debug.log
ここに私の観察可能な
//save the in-memory stream as a local variable so it returns the same instance
let current$ = this.$loggerService.applicationLog$
this.logs$ = this.logSeveritySubject
.asObservable()
.startWith(this.applicationLogName) //the currently selected value
.flatMap((fileName: string) => {
if (fileName === "current") {
return current$;
}
return this.$localStorageService.readAsStringAsync(filename).map((s) => {
let a: any[] = s.split(/\r\n|\r|\n/).filter(n => n.length > 0);
return a.reverse();
});
})
.merge(this.clearLogSubject.asObservable()) //used to reset the scan back to an empty array
.scan((x, y) => {
if (y === null) return [];
return y.concat(x);
}, []);
のための私のセットアップコードは、私は、今です件名に新しいログファイル名をプッシュ
this.clearLogSubject.next(null); //reset the scan back to an empty array
this.logSeveritySubject.next(this.applicationLogName); //read from the user selected option
問題は私が持っている2つのストリームの間で重複エントリが返されます(メモリ内ストリームはおそらく完了しないため?)。 return current$;
が複数回実行されると、実際には同じインスタンスが最後の観測可能なストリームに複数回置かれると思うようになります。
これをコードするには、おそらくより良い方法があります。私は基本的には、どのログソースを表示するかをユーザーが選択する必要があります。唯一の注意点は、メモリ内のオブザーバブルがいつでも閉じることができないためです。
おそらくそれは私のものですが、RxJsライブラリの良い例があまりにも欠けています。答えをありがとう。 – Matt