RXJS 为什么只有最后一个数字同时具有 a 和 b?
RXJS Why only the last number has both a and b?
我看到了下面的代码:
var source = Rx.Observable
.range(1, 3)
.flatMapLatest(function(x) {
return Rx.Observable.from([x + 'a', x + 'b']);
});
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
document.body.innerHTML += 'Next: ' + x + '<br>';
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
document.body.innerHTML += 'Completed';
});
// Next: 1a
// Next: 2a
// Next: 3a
// Next: 3b
// Completed
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.js"></script>
我想了解为什么只有 3/last 值同时具有 a 和 b。
基本上我仍然难以理解Rx.Observable.from以数组作为参数。
谢谢,
萨尔
好了说明完毕。此行为更多地与 flatMapLatest
和 range
有关,而与 from
有关。
基本上range(1,3)
发出同步 它的三个项目。然后 flatMapLatest
将在同一刻度中仅发出第一个值 'x'+a
。在它发出第二个值之前,它需要关闭那个可观察值,因为来自 range
的新值已经到达并且它需要产生新的相应可观察值(你知道 flatMapLatest
当新值到达时关闭当前的 observable 并产生新的。这就是 latest
的意思,不是吗)。
这样直到最后一个值 (3
),在这种情况下没有新值,因此 from
observable 可以发出其完整的值列表。
一些额外有用的链接:
The flatMapLatest operator behaves much like the standard FlatMap operator, except that whenever a new item is emitted by the source Observable, it will unsubscribe to and stop mirroring the Observable that was generated from the previously-emitted item, and begin only mirroring the current one.
我看到了下面的代码:
var source = Rx.Observable
.range(1, 3)
.flatMapLatest(function(x) {
return Rx.Observable.from([x + 'a', x + 'b']);
});
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
document.body.innerHTML += 'Next: ' + x + '<br>';
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
document.body.innerHTML += 'Completed';
});
// Next: 1a
// Next: 2a
// Next: 3a
// Next: 3b
// Completed
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.js"></script>
我想了解为什么只有 3/last 值同时具有 a 和 b。
基本上我仍然难以理解Rx.Observable.from以数组作为参数。
谢谢, 萨尔
好了说明完毕。此行为更多地与 flatMapLatest
和 range
有关,而与 from
有关。
基本上range(1,3)
发出同步 它的三个项目。然后 flatMapLatest
将在同一刻度中仅发出第一个值 'x'+a
。在它发出第二个值之前,它需要关闭那个可观察值,因为来自 range
的新值已经到达并且它需要产生新的相应可观察值(你知道 flatMapLatest
当新值到达时关闭当前的 observable 并产生新的。这就是 latest
的意思,不是吗)。
这样直到最后一个值 (3
),在这种情况下没有新值,因此 from
observable 可以发出其完整的值列表。
一些额外有用的链接:
The flatMapLatest operator behaves much like the standard FlatMap operator, except that whenever a new item is emitted by the source Observable, it will unsubscribe to and stop mirroring the Observable that was generated from the previously-emitted item, and begin only mirroring the current one.