列挙体を展開するために観察可能なRXを変換する方法
-
21-12-2019 - |
質問
私はそのネイティブ列挙子構築物を使って再生中のWebSocketを正常に設定し、文字列を返すコードをいくつか呼び出します。
def operationStatusFeed = WebSocket.using[String] { implicit request =>
val in = Iteratee.ignore[String]
val out = Enumerator.repeatM {
Promise.timeout(operation, 3 seconds)
}
(in, out)
}
.
これで、operation
関数が文字列の代わりにrx.lang.scala.Observable[String]
を返すようにしたい、そしてそれが入力されるとすぐに文字列を出力したいです。この観測可能なplay.api.libs.iteratee.Enumerator
にマッピングする方法は?
解決
Bryan Gilbertから暗黙の変換を使用できます。これは完全にうまく機能しますが、 Bryan Gilbertの変換のバージョン!登録解除は、jeroen kransenからの答えで呼ばれていません(そしてそれは悪い!)。
/*
* Observable to Enumerator
*/
implicit def observable2Enumerator[T](obs: Observable[T]): Enumerator[T] = {
// unicast create a channel where you can push data and returns an Enumerator
Concurrent.unicast { channel =>
val subscription = obs.subscribe(new ChannelObserver(channel))
val onComplete = { () => subscription.unsubscribe }
val onError = { (_: String, _: Input[T]) => subscription.unsubscribe }
(onComplete, onError)
}
}
class ChannelObserver[T](channel: Channel[T]) extends rx.lang.scala.Observer[T] {
override def onNext(elem: T): Unit = channel.push(elem)
override def onCompleted(): Unit = channel.end()
override def onError(e: Throwable): Unit = channel.end(e)
}
.
完了するには、ここで列挙体から観測可能な変換があります。
/*
* Enumerator to Observable
*/
implicit def enumerator2Observable[T](enum: Enumerator[T]): Observable[T] = {
// creating the Observable that we return
Observable({ observer: Observer[T] =>
// keeping a way to unsubscribe from the observable
var cancelled = false
// enumerator input is tested with this predicate
// once cancelled is set to true, the enumerator will stop producing data
val cancellableEnum = enum through Enumeratee.breakE[T](_ => cancelled)
// applying iteratee on producer, passing data to the observable
cancellableEnum (
Iteratee.foreach(observer.onNext(_))
).onComplete { // passing completion or error to the observable
case Success(_) => observer.onCompleted()
case Failure(e) => observer.onError(e)
}
// unsubscription will change the var to stop the enumerator above via the breakE function
new Subscription { override def unsubscribe() = { cancelled = true } }
})
}
.
RX Play
その一方で、Playの繰り返しや列挙者に対処する時間のほとんどは、(ここでのように)WebSocketsを操作するときです。私たちは皆、繰り返しが本当に直感的ではないことに同意しますが、これはおそらくあなたがあなたのプレイプロジェクトでRXを使用している理由です。
その観察から、私はこれを正確にこれを行う widgetManager というライブラリを構築しました:再生中のRXの統合は反復操作を取り除く。
そのライブラリを使用して、あなたのコードは単に次のようにすることができます:
def operationStatusFeed = WebSocket.using[String] { implicit request =>
// you can optionally give a function to process data from the client (processClientData)
// and a function to execute when connection is closed (onClientClose)
val w = new WidgetManager()
w.addObservable("op", operation)
// subscribe to it and push data in the socket to the client (automatic JS callback called)
w.subscribePush("op")
// deals with Iteratees and Enumerators for you and returns what's needed
w.webSocket
}
.
ここではGithubにあります。 Rxplay (貢献は大歓迎です)
他のヒント
私はこのソリューションを Brian Gilbert :
class ChannelObserver[T](chan: Channel[T]) extends Observer[T] {
override def onNext(arg: T): Unit = chan.push(arg)
override def onCompleted(): Unit = chan.end()
override def onError(e: Throwable): Unit = chan.end(e)
override val asJavaObserver: rx.Observer[T] = new rx.Observer[T] {
def onCompleted() {
chan.end()
}
def onError(e: Throwable) {
chan.end(e)
}
def onNext(arg: T) {
chan.push(arg)
}
}
}
implicit def observable2Enumerator[T](obs: Observable[T]): Enumerator[T] = {
Concurrent.unicast[T](onStart = { chan =>
obs.subscribe(new ChannelObserver(chan))
})
}
.
暗黙関数は、追加のコードなしで、観測値を列挙子に変換します。
所属していません StackOverflow