[英]RxJS - Observable does not execute when takeUntil and its notifier observable emits in the same internal observable
Code:代码:
const main$ = of(true);
const int$ = interval(2000);
const notifier$ = new Subject();
main$.pipe(
switchMap(() => int$.pipe(
tap(() => {
// some logic for when to trigger notifier
notifier$.next(1); // after some intervals
}),
takeUntil(notifier$),
)),
tap(() => {// never reach here})
).subscribe(() => {
// never reach here
});
In the above code, takeUntil
stops the interval
when the notifier$
emits but it does not call subscribe()
ever and never reach the operators eg.在上面的代码中,当
notifier$
发出时takeUntil
停止interval
,但它不会调用subscribe()
并且永远不会到达运算符,例如。 tap()
(see code) after takeUntil
. takeUntil
之后的tap()
(参见代码)。 Why is that?这是为什么? Am I missing something?
我错过了什么吗?
the takeUntil
operator emits the values emitted by a source Observable that would be your main$
Observable until a notifier Observable emits a value as it is being done by the notifier$
Subject. takeUntil
操作符发出源 Observable 发出的值,这将是您的main$
Observable,直到通知器 Observable 发出一个值,因为它是由notifier$
主题完成的。 Thus, after notifier$
emits a value on the next
call within the very first iteration of your interval, the takeUntil
prevents the underlying Observable to be propagated any further, meaning that the tap
operator will never be reached.因此,在
notifier$
在间隔的第一次迭代内的next
调用中发出一个值之后, takeUntil
阻止底层的 Observable 进一步传播,这意味着永远不会到达tap
运算符。
If you were using a different implementation for your notifier$
such as timer
, you could observe the tap
operator to be called until the timer elapses and thereby triggering the takeUntil
.如果您对
notifier$
使用不同的实现,例如timer
,您可以观察到要调用的tap
操作符,直到计时器结束并因此触发takeUntil
。
const main$ = of(true);
const int$ = interval(2000);
const notifier$ = timer(10000);
main$.pipe(
switchMap(() => int$.pipe(
tap(() => {
// some logic for when to trigger notifier
}),
takeUntil(notifier$),
)),
tap((result) => {
console.log(result)
})
).subscribe((result) => {
// will be reached four times.
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.