2017-09-25 89 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(); 
    }); 

任何有關如何預先保存值和切換地圖的方法,沒有像上面那樣的一行變量?RxJS,Observable,如何保存值並將地圖切換到另一個

回答

0

我認爲這是運營商

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();  
    }); 

注意,採取(1)將關閉申購,但如果你希望用戶能夠按按鈕多次,認購保存到一個常量和完成後取消訂閱。

+0

什麼需要(1)怎麼辦? –

+0

take(n)限制showHand $的項目數量,並在n項目之後發出complete()。完成此應該會自動停止訂閱。 –

0

你需要是已經可以用的SwitchMap有selectorFunc過載爲每(outerValue,innerValue)的組合行爲:

this.ticker$ 
    .switchMap(
    () => this.showHand$, 
    (tickerValue, switchMap) => tickerValue 
) 
    .subscribe(showHand => { }); 
相關問題