Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
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.

62 lines
2.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.mygame.utils
  2. import cats.kernel.Eq
  3. import cats.syntax.show._
  4. import com.typesafe.scalalogging.Logger
  5. import monix.bio.Task
  6. import monix.execution.Ack
  7. import monix.execution.Cancelable
  8. import monix.execution.cancelables.SingleAssignCancelable
  9. import monix.reactive.Observable
  10. import monix.reactive.OverflowStrategy
  11. object MonixDirectoryWatcher {
  12. import better.files._
  13. import io.methvin.better.files._
  14. private val logger = Logger[MonixDirectoryWatcher.type]
  15. sealed trait WatchEvent extends Product with Serializable
  16. final case class CreateEvent(file: File, count: Int) extends WatchEvent
  17. final case class ModifyEvent(file: File, count: Int) extends WatchEvent
  18. final case class DeleteEvent(file: File, count: Int) extends WatchEvent
  19. object WatchEvent {
  20. implicit val eq = Eq.fromUniversalEquals[WatchEvent]
  21. }
  22. @SuppressWarnings(Array("org.wartremover.warts.Equals"))
  23. def apply(path: os.Path) =
  24. Task.deferAction(implicit s =>
  25. Task(
  26. Observable
  27. .create[WatchEvent](OverflowStrategy.DropNew(50)) { sub =>
  28. import sub.scheduler
  29. val c = SingleAssignCancelable()
  30. val watcher =
  31. new RecursiveFileMonitor(
  32. File(path.toString),
  33. logger = logger.underlying
  34. ) {
  35. override def onCreate(file: File, count: Int) =
  36. if (sub.onNext(CreateEvent(file, count)) == Ack.Stop)
  37. c.cancel()
  38. override def onModify(file: File, count: Int) =
  39. if (sub.onNext(ModifyEvent(file, count)) == Ack.Stop)
  40. c.cancel()
  41. override def onDelete(file: File, count: Int) =
  42. if (sub.onNext(DeleteEvent(file, count)) == Ack.Stop)
  43. c.cancel()
  44. }
  45. watcher.start()(scheduler)
  46. c := Cancelable(() => watcher.stop())
  47. c
  48. }
  49. .publish
  50. .refCount
  51. )
  52. )
  53. }