<commit_before>package visceljs.connection

import org.scalajs.dom
import rescala.default.{Events, Signal, _}
import viscel.shared.Log
import visceljs.Definitions

import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.scalajs.js
import scala.util.{Failure, Success}


object ServiceWorker {
  def register(): Signal[String] = {
    Events.fromCallback[String] { cb =>
      val workerSupported_? = dom.experimental.serviceworkers.toServiceWorkerNavigator(dom.window.navigator).serviceWorker
      Definitions.getDefined(workerSupported_?) match {
        case None                => cb("none")
        case Some(serviceworker) =>
          serviceworker.register("serviceworker.js").toFuture.onComplete {
            case Success(registration) =>
              cb("registered")
              registration.addEventListener("updatefound", (event: js.Any) => {
                val newWorker = registration.installing
                newWorker.addEventListener("statechange", (event: js.Any) => {
                  cb(newWorker.state)
                })
              })
            case Failure(error)        =>
              Log.JS.error(s"serviceworker failed", error)
              cb("failed")
          }
      }
    }.event.latest("init")
  }
}<commit_msg>fix none serviceworker<commit_after>package visceljs.connection

import org.scalajs.dom
import rescala.default.{Events, Signal, _}
import viscel.shared.Log
import visceljs.Definitions

import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.scalajs.js
import scala.util.{Failure, Success}

import scala.scalajs.js.timers._



object ServiceWorker {
  def register(): Signal[String] = {
    transaction() { implicit at =>
      Events.fromCallback[String] { cb =>
        val workerSupported_? = dom.experimental.serviceworkers.toServiceWorkerNavigator(dom.window.navigator).serviceWorker
        Definitions.getDefined(workerSupported_?) match {
          case None                =>
            // this is a bit sad, we can not call cb within the starting transactin,
            // as that forces a new transactin …
            setTimeout(0) { cb("none") }
          case Some(serviceworker) =>
            serviceworker.register("serviceworker.js").toFuture.onComplete {
              case Success(registration) =>
                cb("registered")
                registration.addEventListener("updatefound", (event: js.Any) => {
                  val newWorker = registration.installing
                  newWorker.addEventListener("statechange", (event: js.Any) => {
                    cb(newWorker.state)
                  })
                })
              case Failure(error)        =>
                Log.JS.error(s"serviceworker failed", error)
                cb("failed")
            }
        }
      }.event.latest("init")
    }
  }
}
{'fraction_non_alphanumeric': 0.10668065757257783, 'fraction_numerical': 0.0003497726477789437, 'mean_word_length': 2.283582089552239, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3314305', 'n_tokens_mistral': 773, 'n_tokens_neox': 734, 'n_words': 141}