本文整理汇总了Scala中monix.reactive.Observer类的典型用法代码示例。如果您正苦于以下问题:Scala Observer类的具体用法?Scala Observer怎么用?Scala Observer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Observer类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TestObserver
//设置package包名称以及导入依赖的类
package walfie.gbf.raidfinder.util
import monix.execution.Ack
import monix.reactive.Observer
import scala.concurrent.Future
class TestObserver[T] extends Observer[T] {
var received = Vector.empty[T]
var errorsReceived = Vector.empty[Throwable]
var isComplete: Boolean = false
def onNext(elem: T): Future[Ack] = {
received = received :+ elem
Ack.Continue
}
def onError(e: Throwable): Unit = {
errorsReceived = errorsReceived :+ e
}
def onComplete(): Unit = {
isComplete = true
}
}
示例2: handle
//设置package包名称以及导入依赖的类
package de.m7w3.signal.events
import de.m7w3.signal.Logging
import monix.execution.Ack
import monix.execution.Ack.Continue
import monix.reactive.Observer
import scala.concurrent.Future
trait EventListener extends Observer[SignalDesktopEvent] {
def handle: PartialFunction[SignalDesktopEvent, Unit]
override def onNext(elem: SignalDesktopEvent): Future[Ack] = {
if (handle.isDefinedAt(elem)) {
handle.apply(elem)
}
Continue
}
}
abstract class SimpleEventListener extends EventListener with Logging {
override def onError(ex: Throwable): Unit = {
logger.error(s"Error in event listener ${getClass.getSimpleName}", ex)
}
override def onComplete(): Unit = {
logger.info(s"${getClass.getSimpleName} completed")
}
}
object EventListener {
def simple(onEvent: PartialFunction[SignalDesktopEvent, Unit]): EventListener = {
new SimpleEventListener() {
override def handle: PartialFunction[SignalDesktopEvent, Unit] = onEvent
}
}
}