“非测试”中的 Observable.never()生产代码并且没有明确处理?
代码如下:
PublishSubject.create<Pojo>
.doOnNext{
//..
}
.debounce {
if (timeout > 0) Observable.timer(timeout, TimeUnit.MILLISECONDS)
else Observable.never()
}
可以看出,有一个 Observable.never() ,我的理解是,当超时为负数时,不会触发任何内容。
根据 Observable.never()
文档:
返回一个从不发送任何项目或通知的 Observable 观察员。这个 ObservableSource 主要用于测试 目的。
调度程序:默认情况下从不对特定的任务进行操作 调度程序。类型参数:T - 发射的(未)项目的类型 ObservableSource 返回:一个从不发出任何项目的 Observable 或 向观察者发送任何通知
“主要用于测试目的”让我感到困惑。上面的例子对于 never() 有效吗?既然没有明确处理,那可以吗?
Following is coded:
PublishSubject.create<Pojo>
.doOnNext{
//..
}
.debounce {
if (timeout > 0) Observable.timer(timeout, TimeUnit.MILLISECONDS)
else Observable.never()
}
As it can be seen, there is an Observable.never()
, what I understand as nothing shall be triggered when timeout is negative.
Accroding to Observable.never()
documentation:
Returns an Observable that never sends any items or notifications to
an Observer. This ObservableSource is useful primarily for testing
purposes.Scheduler: never does not operate by default on a particular
Scheduler. Type Parameters: T - the type of items (not) emitted by the
ObservableSource Returns: an Observable that never emits any items or
sends any notifications to an Observer
The "useful primarily for testing purposes" confused me. Is the above example valid with never()? Since it is not disposed explcitly, it that ok?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
never()
永远不会发出信号,因此当您在某处需要一个Observable
但实际上并不需要发出信号的项目或完成时,您可以在那里使用它。有时我们需要测试某些运算符状态,这些状态需要 Observable 作为参数,但根本不触发任何值(例如,创建无尽的窗口)。在您的用例中,这可能是错误的,因为
debounce
会在该内部Observable
的持续时间内抑制当前项目之后的项目。该持续时间的结束是来自Observable
的信号,但never
永远不会发出信号,因此所有后续项目都将被丢弃,并且debounce
获胜没有发出任何信号,甚至没有完成。问题是,在
timeout <= 0
内,您打算如何处理当前项目之后的下一个项目?如果您不想抑制下一项,只需返回 Observable.empty() 即可。never()
never signals thus when you need anObservable
somewhere but don't really need an item or completion to be signaled, you can use it there. Sometimes we need to test certain operator states which need anObservable
as a parameter but not trigger any value at all (for example, creating endless windows).In your use case, it is probably wrong because
debounce
suppresses items coming after the current item for the duration of that innerObservable
. The end of that duration is a signal from theObservable
butnever
doesn't ever signal, thus all subsequent items will be dropped anddebounce
won't signal anything, not even completion.The question is, what did you intend to do with the next item following the current current item for
timeout <= 0
? If you don't want to suppress the next item, just returnObservable.empty()
instead.