2017-09-25 64 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 => { }); 
相关问题