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.5 KiB

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