简体   繁体   English

如何在fs2功能流中为Scala创建离散流?

[英]How to create discrete streams in fs2 Functional Stream for Scala?

Is it possible to create stream of discrete events in fs2 ? 是否可以在fs2中创建离散事件流? if so how to do it. 如果是这样的话怎么做。 I just started to play with the library and I know I have a lot to study. 我刚刚开始和图书馆一起玩,我知道我有很多需要学习的东西。 But I am not seeing any example related. 但我没有看到任何相关的例子。 eg I would like to create a stream for " mousemove " or " click " in scalajs or swing . 例如,我想在scalajsswing中为“ mousemove ”或“ click ”创建一个流。 I am looking for something like in RxJS that I can use a Rx.Observable.create to create discrete events something like: 我正在寻找类似于RxJS的东西,我可以使用Rx.Observable.create创建离散事件,如:

//note: pseudo code
var mouse = Rx.Observable.create( subscriber => {
     document.body.addEventListener("mousemove", event =>{
      subscriber.onNext(event)
 })
} ) 

The equivalent in fs2 might not be so trivial but if anyone can suggest me how. fs2中的等价物可能不是那么微不足道,但如果有人可以建议我如何。 I guess it would be using Handler and Pull/Push datatypes but I am far to understand how. 我想它会使用HandlerPull / Push数据类型,但我很清楚如何。

Cheers. 干杯。

Here's an example I came up with which demonstrates how to use fs2 with JavaFX: 这是我提出的一个示例,它演示了如何在JavaFX中使用fs2:

import cats.implicits._
import cats.effect._
import cats.effect.implicits._
import javafx.application.{Application, Platform}
import javafx.scene.{Node, Scene}
import javafx.scene.layout._
import javafx.stage.Stage
import fs2._
import fs2.concurrent._
import javafx.beans.value.WritableValue
import javafx.scene.control.{Label, TextField}
import javafx.scene.input.KeyEvent

import scala.concurrent.ExecutionContext

import scala.util.Try

class Fs2Ui extends Application {
  override def start(primaryStage: Stage): Unit = {
    implicit val cs: ContextShift[IO] = IO.contextShift(ExecutionContext.global)
    implicit val timer: Timer[IO] = IO.timer(ExecutionContext.global)

    new Logic[IO]().run(primaryStage).start.unsafeRunSync()
  }

  class Logic[F[_]: ConcurrentEffect: ContextShift: Timer] {
    import Fs2Ui._
    import java.time.{Duration, Instant}
    import java.util.concurrent.TimeUnit.MILLISECONDS

    def run(primaryStage: Stage): F[Unit] = for {
      v <- initializeUi(primaryStage)
      View(input, feedback) = v

      _ <- Stream(input).covary[F]
        .through(typedChars)
        .through(processInput)
        .through(displayFeedback(feedback.textProperty))
        .compile.drain
    } yield ()

    private def initializeUi(primaryStage: Stage): F[View] = updateUi {
      val input = new TextField()
      input.setPrefWidth(300)
      val feedback = new Label("...")

      val vbox = new VBox(input, feedback)
      val root = new StackPane(vbox)
      val scene = new Scene(root)

      primaryStage.setScene(scene)
      primaryStage.show()

      View(input, feedback)
    }

    private def processInput: Pipe[F, TypedChar, Feedback] = for {
      typed <- _
      _ <- Stream.eval(ContextShift[F].shift)
      res <- Stream.eval { time(processSingle(typed)) }
      (d, Feedback(str)) = res
    } yield Feedback(s"$str in [$d]")

    private def displayFeedback(value: WritableValue[String]): Pipe[F, Feedback, Unit] =
      _.map { case Feedback(str) => str } through updateValue(value)

    private def time[A](f: F[A]): F[(Duration, A)] = {
      val now = Timer[F].clock.monotonic(MILLISECONDS).map(Instant.ofEpochMilli)
      for {
        start <- now
        a <- f
        stop <- now
        d = Duration.between(start, stop)
      } yield (d, a)
    }

    private val processSingle: TypedChar => F[Feedback] = {
      import scala.util.Random
      import scala.concurrent.duration._

      val prng = new Random()
      def randomDelay: F[Unit] = Timer[F].sleep { (250 + prng.nextInt(750)).millis }

      c => randomDelay *> Sync[F].delay(Feedback(s"processed $c"))
    }
  }
}

object Fs2Ui {
  case class View(input: TextField, feedback: Label)

  case class TypedChar(value: String)
  case class Feedback(value: String)

  private def typedChars[F[_]: ConcurrentEffect]: Pipe[F, Node, TypedChar] = for {
    node <- _
    q <- Stream.eval(Queue.unbounded[F, KeyEvent])
    _ <- Stream.eval(Sync[F].delay {
      node.setOnKeyTyped { evt => (q enqueue1 evt).toIO.unsafeRunSync() }
    })
    keyEvent <- q.dequeue
  } yield TypedChar(keyEvent.getCharacter)

  private def updateValue[F[_]: Async, A](value: WritableValue[A]): Pipe[F, A, Unit] = for {
    a <- _
    _ <- Stream.eval(updateUi(value setValue a))
  } yield ()

  private def updateUi[F[_]: Async, A](action: => A): F[A] =
    Async[F].async[A] { cb =>
      Platform.runLater { () =>
        cb(Try(action).toEither)
      }
    }
}

The specific parts that demonstrate bindings between fs2 and JavaFX are the two Pipe s: typedChars and updateValue . 演示fs2和JavaFX之间绑定的特定部分是两个Pipe s: typedCharsupdateValue Personally, I think, the most challenging part was adapting a KeyEvent listener to look like an fs2 Stream of events: 我个人认为,最具挑战性的部分是使KeyEvent监听器看起来像fs2事件Stream

node.setOnKeyTyped { evt => (q enqueue1 evt).toIO.unsafeRunSync() }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM