Javascript RxJS CombinelateTest运算符异常行为
我有一个关于RxJS CombineTest运算符的查询。我已修改了中给出的示例 详情如下:Javascript RxJS CombinelateTest运算符异常行为,javascript,angular,rx-java,rxjs5,Javascript,Angular,Rx Java,Rxjs5,我有一个关于RxJS CombineTest运算符的查询。我已修改了中给出的示例 详情如下: //timerOne emits first value at 1s, then once every 4s const timerOne = Rx.Observable.timer(1000, 4000); //timerTwo emits first value at 2s, then once every 4s const timerTwo = Rx.Observable.timer(2000,
//timerOne emits first value at 1s, then once every 4s
const timerOne = Rx.Observable.timer(1000, 4000);
//timerTwo emits first value at 2s, then once every 4s
const timerTwo = Rx.Observable.timer(2000, 4000)
//timerThree emits first value at 3s, then once every 4s
const timerThree = Rx.Observable.of(false);
//when one timer emits, emit the latest values from each timer as an array
const combined = Rx.Observable
.combineLatest(
timerOne,
timerTwo,
timerThree
);
const subscribe = combined.subscribe(latestValues => {
//grab latest emitted values for timers one, two, and three
const [timerValOne, timerValTwo, timerValThree] = latestValues;
if(latestValues[0] === 3) {
this.timerThree = Rx.Observable.of(true);
}
console.log(
`Timer One Latest: ${timerValOne},
Timer Two Latest: ${timerValTwo},
Timer Three Latest: ${timerValThree}`
);
});
我希望TimerTree的值更改为true位,它始终保持打印false,如输出片段所示:
"Timer One Latest: 3,
Timer Two Latest: 2,
Timer Three Latest: false"
"Timer One Latest: 3,
Timer Two Latest: 3,
Timer Three Latest: false"
"Timer One Latest: 4,
Timer Two Latest: 3,
Timer Three Latest: false"
知道为什么会这样吗?有办法解决这个问题吗?感谢这里需要注意的重要一点是,
TimerTree
本身不是一个可观察的对象,而是对可观察对象的引用。使用combinelatetest
时,它是在组合该对象,而不是引用该对象的变量。因此,当您将timerTree
指定给一个新的可观察对象时,它现在指向一个新对象,但组合的
仍在使用旧对象
如果您希望能够更改
TimerTree
的值,请尝试使用主题
。然后,您可以使用TimerTree向其推送新值。接下来
补充John的答案:
this.timerTree
在最新值[0]==3
时未定义,因为在lambda函数内部时,this
指的是“最近的外部范围”
如果要在浏览器中运行此操作,则此
将是窗口
对象,因此您只需向窗口对象添加属性
另外,timerTree
被定义为const
,这意味着如果您尝试对同一对象进行重新分配(但如上所述,您正在分配给不同的对象),它将抛出错误
通过摆弄fiddle,我得到了一些您想要的东西,尽管这需要工作来消除代码重复:
//timerOne emits first value at 1s, then once every 4s
const timerOne = Rx.Observable.timer(1000, 4000);
//timerTwo emits first value at 2s, then once every 4s
const timerTwo = Rx.Observable.timer(2000, 4000)
//timerThree emits first value at 3s, then once every 4s
let timerThree = Rx.Observable.timer(3000, 4000)
//when one timer emits, emit the latest values from each timer as an array
let combined = Rx.Observable
.combineLatest(
timerOne,
timerTwo,
timerThree
);
const subscribe = combined.subscribe(latestValues => {
//grab latest emitted values for timers one, two, and three
const [timerValOne, timerValTwo, timerValThree] = latestValues;
if(latestValues[0] === 3) {
console.log("this ===>", this);
console.log("this.timerThree ===> ", this.timerThree);
subscribe.unsubscribe();
combined = Rx.Observable.combineLatest(timerOne, timerTwo, Rx.Observable.of(true));
combined.subscribe(lvs => {
const [tv1, tv2, tv3] = lvs
console.log(
`Timer One Latest: ${tv1},
Timer Two Latest: ${tv2},
Timer Three Latest: ${tv3}`
);
})
}
console.log(
`Timer One Latest: ${timerValOne},
Timer Two Latest: ${timerValTwo},
Timer Three Latest: ${timerValThree}`
);
});
请注意unsubscribe()
调用以防止以前组合的计时器再次执行,新调用combinelateest
和新的observeable
以打印true
我还必须将timerTree
从const
更改为let
,以便能够重新分配它