Periodic job stops when window's default visiblity is set to false. #12973
-
I made a demo app, which periodically destroy existing WebviewWindow and recreate to load other url. It works as expected when main window's visiblity is set to true. When I hide window after launch it works well too. And is destroying and recreating WebviewWindow every time the best way for loading specific url without using Rust?
Following is source code. It's written in Scala using fs2. object T extends IOApp:
// utility functions
inline def clog(msg: String) = coreMod.invoke("cprint", SMap("message" -> msg))
extension [A](p:scalajs.js.Promise[A])
def toIOEither: IO[Either[String, A]] =
val success = (v: A) => v.asRight[String]
val error = (e: Any) => e.asInstanceOf[String].asLeft[A]
IO.fromFuture(IO(p.`then`(success, error).toFuture))
def toIO: IO[A] =
IO.fromFuture(IO(p.`then`(v => v, e => clog(e.asInstanceOf[String])).toFuture))
// main entry
def run(args: List[String]): IO[ExitCode] =
for
_ <- hideMainWindow
_ <- setupTimer
yield ExitCode.Success
def hideMainWindow =
clog("function: hideMainWindow").toIO >>
windowMod.Window.getByLabel("main").toIO.flatMap: mainWin =>
clog(s"got window[${mainWin.label}], hiding...")
mainWin.hide().toIO
def setupTimer =
val timer = dom.document.getElementById("counter")
val msg = dom.document.getElementById("message")
// call tick function every second
Stream.awakeEvery[IO](1.second).evalTap(t => tick(timer, msg)(t)).compile.drain
val interval = 5
val sites = Seq(
("Tauri", "https://tauri.app"),
("Scala.js", "https://scala-js.org"),
("FS2", "https://fs2.io")
)
val (winw, winh) = (800, 600)
val targetWinLabel = "targetWebViewWindow"
def tick(time: dom.Element, msg: dom.Element)(t: FiniteDuration): IO[Unit] =
// branch IO according to sec
def doAction(sec: Long) = (sec % interval) match
case 0L => setUrl(sec) // run setUrl every interval seconds
case 1L => IO(msg.innerText = "") // erase msg after 1 second
case _ => IO.unit
def setUrl(sec: Long) =
IO:
val nexti = ((sec / interval) % sites.length).toInt
msg.innerText = s"It's time. Let's load ${sites(nexti)._1}"
clog(s"It's time. Let's load ${sites(nexti)._1}")
sites(nexti)._2
.flatMap: url =>
navigateToUrl(url)
def navigateToUrl(url: String) =
for
_ <- destroyWebViewWindow
_ <- createWebViewWindow(url)
yield ()
def destroyWebViewWindow =
webviewWindowMod.WebviewWindow.getByLabel(targetWinLabel).toIO.flatMap: wvwOrNull =>
wvwOrNull match
case wvw: webviewWindowMod.WebviewWindow =>
clog(s"webviewWindow exists, destroying ${wvw.label}")
wvw.destroy().toIO
case null =>
clog(s"webviewWindow ($targetWinLabel) doesn't exist").toIO
IO.unit
def createWebViewWindow(url: String) =
IO:
val wvwo = WebviewWindowOption().setUrl(url)
clog(s"creating window with ${url} => ${wvwo.url}")
new webviewWindowMod.WebviewWindow(targetWinLabel, wvwo)
.flatMap: wvw =>
clog(s"showing window! ${wvw.label}")
wvw.show().toIO
val p =
IO:
val sec = t.toSeconds
time.innerText = s"${sec}sec"
clog(s"Tick... ${sec}sec")
sec
.flatMap: s =>
doAction(s)
p |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
probably #5250 |
Beta Was this translation helpful? Give feedback.
probably #5250