2017-09-25 12 views
0
// ticker$ will update every 3s 
// showHand$ will only triger after user click button 
// I would like to take last ticker price as user order price when user click button 

let lastPrice: number; 

this.ticker$ 
    // What I am doing now is preserve value to vairable here. 
    .do(ticker => lastPrice = ticker.closePrice) 
    .switchMap(() => this.showHand$) 
    .subscribe(showHand => { 
    // use value here 
    this.order.price = lastPrice; 
    this.order.amount = showHand.amount; 
    this.order.type = showHand.type; 

    this.submit(); 
    }); 

上記のような1行の変数を使用せずに、値を事前に設定してマップを切り替える方法については、RxJS、値を保存して別のマップに切り替える方法

答えて

0

私は(1)サブスクリプションを閉じていますが、ユーザーがボタンを何度も押すことができるようにしたい場合は、constのにサブスクリプションを保存します取る、これはオペレータ

this.showHand$.take(1) 
    .withLatestFrom(this.ticker$) 
    .subscribe(([showHand, ticker]) => { 
    this.order.price = ticker.closePrice; 
    this.order.amount = showHand.amount; 
    this.order.type = showHand.type; 
    this.submit();  
    }); 

注意だと思います完了したら退会してください。

+0

取るんどのような(1)でしょうか? –

+0

take(n)は、showHand $からアイテムの数を制限し、n個のアイテムの後にcomplete()を送出します。これを完全にすると自動的にサブスクリプションが停止されます。 –

0

必要な行動は、すべての(outerValue,innerValue)の組み合わせのためのselectorFuncとSwitchMapの過負荷で既に可能である:

this.ticker$ 
    .switchMap(
    () => this.showHand$, 
    (tickerValue, switchMap) => tickerValue 
) 
    .subscribe(showHand => { }); 
関連する問題