admin管理员组文章数量:1332881
I am trying to figure out, how mergeAll
works and created examples:
const clicks = Rx.Observable.interval(4000).map(()=> "first");
const higherOrder = clicks.map((ev) => Rx.Observable.interval(1000).map(() => "inner").take(10));
const firstOrder = higherOrder.mergeAll();
firstOrder.subscribe(x => console.log(x));
the output it always inner
and first
never outputted. After calling mergeAll()
the clicks
observable is no more relevant?
On more example:
const input = document.getElementById("window");
const clicks = Rx.Observable.fromEvent(input, 'keyup').map(() => "Hello");
const interval = Rx.Observable.interval(4000);
const result = clicks.window(interval)
.map(win => {
return win.take(1);
})
.mergeAll(); // flatten the Observable-of-Observables
result.subscribe(x => console.log("Result " + x));
on subscribe, I've got the result from outer observable "Result Hello" not the inner observable. What kind of role plays mergeAll
in this case?
Why the win
variable is an instance observable
not Hello
?
I am trying to figure out, how mergeAll
works and created examples:
const clicks = Rx.Observable.interval(4000).map(()=> "first");
const higherOrder = clicks.map((ev) => Rx.Observable.interval(1000).map(() => "inner").take(10));
const firstOrder = higherOrder.mergeAll();
firstOrder.subscribe(x => console.log(x));
the output it always inner
and first
never outputted. After calling mergeAll()
the clicks
observable is no more relevant?
On more example:
const input = document.getElementById("window");
const clicks = Rx.Observable.fromEvent(input, 'keyup').map(() => "Hello");
const interval = Rx.Observable.interval(4000);
const result = clicks.window(interval)
.map(win => {
return win.take(1);
})
.mergeAll(); // flatten the Observable-of-Observables
result.subscribe(x => console.log("Result " + x));
on subscribe, I've got the result from outer observable "Result Hello" not the inner observable. What kind of role plays mergeAll
in this case?
Why the win
variable is an instance observable
not Hello
?
2 Answers
Reset to default 7After calling mergeAll() the clicks observable is no more relevant?
Correct. You map each individual click to a stream of "inner"
events. mergeAll
simply merges those streams together. The click event lives in this resulting stream only very faintly as the point in time where a specific merged stream starts. It bees a bit more clear this way:
const clicks$ = Rx.Observable.interval(1000);
const higherOrder$ = clicks$.map(click => Rx.Observable.interval(500)
.map(counter => `${click}–${counter}`)
);
higherOrder$.mergeAll().subscribe(console.log);
The documentation and its marble diagram might also help you understand:
you should use switchMap.
firstOrder.switchMap(x => console.log(x)).subscribe( value => console.log(value));
or
result.switchMap(x => console.log("Result " + x)).subscribe( value => console.log(value));
switch expects a stream of Observables
, when it get an Observable pushed onto it’s input stream it unsubscribes from any previous Observables and subscribes to the new one and then emits any values from that Observable onto it’s output stream.
本文标签: javascriptHow does the mergeAll worksStack Overflow
版权声明:本文标题:javascript - How does the mergeAll works? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742297081a2448949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论