nak/src/main/scala/Store.scala

47 lines
1.1 KiB
Scala
Raw Normal View History

2023-03-24 21:12:11 -04:00
import cats.data.*
import cats.effect.*
import cats.effect.syntax.all.*
import cats.syntax.all.*
import fs2.concurrent.*
import fs2.dom.{Event => _, *}
import scoin.PrivateKey
2023-03-24 21:12:11 -04:00
case class Store(
input: SignallingRef[IO, String],
result: SignallingRef[IO, Result]
)
object Store {
def apply(window: Window[IO]): Resource[IO, Store] = {
val key = "nak-input"
for {
input <- SignallingRef[IO].of("").toResource
result <- SignallingRef[IO, Result](Left("")).toResource
_ <- Resource.eval {
OptionT(window.localStorage.getItem(key))
.foreachF(input.set(_))
}
_ <- window.localStorage
.events(window)
.foreach {
case Storage.Event.Updated(`key`, _, value, _) =>
input.set(value)
case _ => IO.unit
}
.compile
.drain
.background
_ <- input.discrete
.evalTap(input => IO.cede *> window.localStorage.setItem(key, input))
.evalTap(input => result.set(Parser.parseInput(input.trim())))
2023-03-24 21:12:11 -04:00
.compile
.drain
.background
} yield Store(input, result)
}
}