You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.6 KiB

  1. package nova.monadic_sfx.ui
  2. import scala.concurrent.duration._
  3. import akka.actor.typed._
  4. import akka.util.Timeout
  5. import com.softwaremill.macwire._
  6. import io.odin.Logger
  7. import monix.eval.Task
  8. import monix.execution.Callback
  9. import monix.execution.Scheduler
  10. import nova.monadic_sfx.AppTypes
  11. import nova.monadic_sfx.actors.Counter
  12. import nova.monadic_sfx.executors.Schedulers
  13. import nova.monadic_sfx.http.Requesters
  14. import scalafx.application.JFXApp
  15. class MyFxAppOld(
  16. logger: Logger[Task],
  17. backend: AppTypes.HttpBackend,
  18. actorSystem: ActorSystem[SpawnProtocol.Command],
  19. requesters: Requesters,
  20. schedulers: Schedulers
  21. ) extends JFXApp {
  22. implicit lazy val defaultScheduler: Scheduler = schedulers.fx
  23. // lazy val fxActor: Task[ActorRef[Counter.Command]] = wireWith(
  24. // MyFxApp.makeCounterActor _
  25. // )
  26. lazy val application =
  27. for {
  28. appStage <- Task(wireWith(UiModule.makePrimaryStage _))
  29. // _ <- Task {
  30. // val counterActor = testActor(actorSystem)
  31. // counterActor ! (Counter.Increment)
  32. // }
  33. // ta <- testActor2(actorSystem)
  34. // actor <-
  35. // actorTask.bracket(actor => Task(actor ! (Counter.Increment)))(actor =>
  36. // Task(actor ! (Counter.Stop))
  37. // )
  38. // actor <- actorTask
  39. // actor <- fxActor
  40. // _ <- Task(actor ! Counter.Increment)
  41. _ <- Task { stage = appStage }
  42. _ <- Task.sleep(2.seconds)
  43. // loginScene <- wire[LoginScreen].render
  44. // _ <- Task {
  45. // // appStage.maximized = true
  46. // appStage.height = 800
  47. // appStage.width = 800
  48. // appStage
  49. // .scene()
  50. // .setRoot(
  51. // loginScene
  52. // )
  53. // }
  54. } yield ()
  55. // def testActor(
  56. // system: ActorSystem
  57. // ): akka.actor.typed.ActorRef[Counter.Command] = {
  58. // val behaviour: Behavior[Counter.Command] =
  59. // Behaviors.setup(context => wire[Counter])
  60. // system.spawn(
  61. // behaviour,
  62. // "CounterActor",
  63. // DispatcherSelector.fromConfig("javafx-dispatcher")
  64. // )
  65. // }
  66. application.timed.runAsync(
  67. new Callback[Throwable, (FiniteDuration, Unit)] {
  68. override def onSuccess(value: (FiniteDuration, Unit)): Unit = {
  69. val (duration, _) = value
  70. println(
  71. s"Application started successfully in ${duration.toSeconds} seconds"
  72. )
  73. }
  74. override def onError(e: Throwable): Unit = {
  75. println("Application start failed. Reason -")
  76. e.printStackTrace()
  77. }
  78. }
  79. )
  80. override def stopApp() = {
  81. // val stop = for {
  82. // actor <- fxActor
  83. // _ <- logger.info("Stopping actor counter")
  84. // // _ <- Task.fromFuture { actor.ask[Counter.Value](Counter.GetValue) }
  85. // t <- Task(actor ! Counter.Stop)
  86. // // _ <- Task.sleep(1.second)
  87. // _ <- logger.info("Counter actor stopped")
  88. // } yield ()
  89. // stop.runAsyncAndForget
  90. // // Platform.exit()
  91. }
  92. }
  93. object MyFxAppOld {
  94. def makeCounterActor(
  95. system: ActorSystem[SpawnProtocol.Command],
  96. logger: Logger[Task]
  97. ): Task[ActorRef[Counter.Command]] = {
  98. import akka.actor.typed.scaladsl.AskPattern._
  99. import scala.concurrent.ExecutionContext
  100. implicit val timeout: Timeout = Timeout(3.seconds)
  101. implicit val ec: ExecutionContext = system.executionContext
  102. implicit val scheduler = system.scheduler
  103. Task.fromFuture {
  104. system.ask(
  105. SpawnProtocol.Spawn(
  106. behavior = wireWith(Counter.apply _),
  107. name = "counterActor",
  108. DispatcherSelector.fromConfig("javafx-dispatcher"),
  109. _
  110. )
  111. )
  112. }
  113. }
  114. }