作成するのに費用がかかるオブザーバブルがあるので、shared
です。しかし、場合によっては、すべての加入者が加入停止し、その後すぐに(または少し遅れて)新しい加入者が加入する。rxjs5:共有オブザーバブルの登録解除を延期する
実際に観察はここに複製するにはあまりにも複雑であるが、議論のために:
const heavyObservable = Rx.Observable.create((observer) => {
console.log('I am expensive, avoid hitting this code');
return Rx.Observable
.interval(500) // these updates are cheap though!
.subscribe(observer)
.add(() => {
console.log('Cache has been destroyed, will have to be rebuild on next call');
});
});
私は、この観測可能の作成に関与高価なコードをヒットする必要はありません。 n msまで切断を延期したいと思います。これを行う方法はありますか?
const sharedObservable = heavyObservable
.publish()
// ideally I'm looking for a way to get refCount to wait for new
// subscribers for n ms before unsubscribing when refcount === 0
.refCount();
// calling subscribe here invokes heavyObservable which can take a bit of time
const subscription1 = sharedObservable.subscribe();
// log: I am expensive, avoid hitting this code
// second call is "free" - the underlying observable is reused
const subscription2 = sharedObservable.subscribe();
subscription1.unsubscribe();
subscription2.unsubscribe();
// calling subscribe again here invokes heavyObservable over again
const subscription3 = sharedObservable.subscribe();
// log: I am expensive, avoid hitting this code
は、それを変換し、これまでにそれを保持します。変換後のデータが変更されるたびに出力されます。変換は最初は高価ですが、維持するのは安いです。私は、サーバー接続を永続的に開いたままにする接続を使用することができます。可能であれば、私は切断したいと思います。ほとんどの場合、これは正常に動作しますが、私は急速なunsub&resubがあるいくつかのエッジケースを持っています。私は明示的な、長寿命のサブを追加することでこれを呼び出し側で処理できますが、これは覚えておくことと余分なことです。私はむしろ、観察可能なサブスクリプション "スムージング"を処理したいと思う – studds
あなたはそのストリームを投稿できますか? - 完全な絵なしで適切な助けをすることはほとんど不可能です(もちろん、あなたはURLを変更することができます..) – olsn
私は大好きですが、ここで複製するには大きすぎます。明らかに、コードを実行するのが安くて、それを実行する必要性を避けることができれば、それが最適ですが、それはスタックオーバーフローの問題の範囲を超えています。ミッキーマウスの例を追加して、目的を少し詳しく説明しようとしました。 – studds