admin 管理员组文章数量: 1184232
// 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();
});
Any segestion about how to prevser value and switch map together, without one line variable like above?
// 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();
});
Any segestion about how to prevser value and switch map together, without one line variable like above?
Share Improve this question asked Sep 25, 2017 at 4:57 Long ZhaoLong Zhao 1531 gold badge1 silver badge9 bronze badges 1- 1 Please consider changing the accepted answer to the result selector solution posted by Cameron. There is no need to use another operator and withLatestFrom has certain caveats. – Christof Commented Oct 24, 2019 at 7:41
4 Answers
Reset to default 15Results selector function is deprecated in version 6 will be removed in version 7.
From docs:
https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#result-selectors
with resultSelector (v5.x)
source.pipe(
switchMap(fn1, fn2)
)
the same functionality without resultSelector, achieved with inner map
source.pipe(
switchMap((a, i) => fn1(a, i).pipe(
map((b, ii) => fn2(a, b, i, ii))
)
)
The behaviour you require is already possible with an overload of SwitchMap with a selectorFunc for the combination of every (outerValue,innerValue):
this.ticker$
.switchMap(
() => this.showHand$,
(tickerValue, switchMap) => tickerValue
)
.subscribe(showHand => { });
There is a little hack to achieve this - basically you have a whole new observable inside the switchmap, and this observable has access to the value passed into the switchmap function. You can use this value in an inner map to preserve the value.
this.ticker$
.switchMap(ticker => this.showHand$.pipe(map(hand => ({ ticker,hand }) ))
.subscribe( obj => {
// use value here
this.order.price = obj.ticker;
this.order.amount = obj.hand.amount;
this.order.type = obj.hand.type;
this.submit();
});
I think this is the operator
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();
});
Note, take(1) will close subscription, but if you want the user to be able to press the button many times, save the subscription to a const and unsubscribe when finished.
本文标签:
版权声明:本文标题:javascript - RxJS, Observable, how to preserve value and switch map to another one - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1738248788a1949854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论