<commit_before>package todo

import io.circe.generic.auto._
import io.circe.syntax._
import loci.communicator.experimental.webrtc.WebRTC.ConnectorFactory
import loci.communicator.experimental.webrtc._
import loci.registry.{Binding, Registry}
import loci.serializer.circe._
import org.scalajs.dom.{UIEvent, document}
import rescala.debuggable.ChromeDebuggerInterface
import rescala.restoration.LocalStorageStore
import rescala.lattices.sequences.RGOA
import scalatags.JsDom.all._
import scalatags.JsDom.tags2.section

import scala.concurrent.{Future, Promise}
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.util.Try


object Todolist {


  type TodoTransfer = RGOA[taskHandling.Taskref]

  implicit val storingEngine: LocalStorageStore = new LocalStorageStore()
  import storingEngine._

  val taskHandling = new TaskHandling
  val todoApp = TodoApp(taskHandling)

  val registry = new Registry
  val crdtDescriptions = Binding[TodoTransfer => Unit]("taskData")

  val remotePromise = Evt[TodoTransfer => Future[Unit]]

  def main(args: Array[String]): Unit = {

    ChromeDebuggerInterface.setup(storingEngine)

    val remote = Events.fromCallback[RGOA[taskHandling.Taskref]](
      cb => registry.bind(crdtDescriptions) { td: TodoTransfer =>
        println(s"receiving tasklist $td")
        Try{cb(td)}.failed.foreach{println}
        println(s"caled cb")
      })

    remote.event.observe(tl => println(s"received $tl"))

    val todores = todoApp.getContents(remote.event)

    document.body.replaceChild(todores.div.render, document.body.firstElementChild)
    document.body.appendChild(webrtchandlingArea.render)

    remotePromise.observe { f =>
      todores.tasklist.observe{s =>
        println(s"sending tasklist $s")
        f(s)
      }
    }

    ChromeDebuggerInterface.finishedLoading()
  }




  def webrtchandlingArea: Tag = {

    val renderedTa = textarea().render
    val renderedPre = pre().render

    var pn: Option[PendingNonesense] = None

    val hb = button("host", onclick := { uie: UIEvent =>
      val res = webrtcIntermediate(WebRTC.offer())
      res.session.foreach(s => renderedPre.textContent = s.asJson.noSpaces)
      pn = Some(res)
      val remoteFut = registry.connect(res.connector)
      remoteFut.foreach { remote =>
        val res = registry.lookup(crdtDescriptions, remote)
        println(s"host connected $res")
        remotePromise.fire(res)
      }
    })


    val cb = button("connect", onclick := { uie: UIEvent =>
      val cs = io.circe.parser.decode[WebRTC.CompleteSession](renderedTa.value).right.get
      (pn match {
        case None     => // we are client
          val res = webrtcIntermediate(WebRTC.answer())
          res.session.foreach(s => renderedPre.textContent = s.asJson.noSpaces)
          registry.connect(res.connector)
          res.connector
        case Some(ss) => // we are server
          ss.connector
      }) set cs
    })

    section(hb, cb, renderedPre, renderedTa)
  }




  case class PendingNonesense(connector: WebRTC.Connector,
                         session: Future[WebRTC.CompleteSession])

  def webrtcIntermediate(cf: ConnectorFactory) = {<commit_msg>sync in both directions<commit_after>package todo

import io.circe.generic.auto._
import io.circe.syntax._
import loci.communicator.experimental.webrtc.WebRTC.ConnectorFactory
import loci.communicator.experimental.webrtc._
import loci.registry.{Binding, Registry}
import loci.serializer.circe._
import loci.transmitter.RemoteRef
import org.scalajs.dom.{UIEvent, document}
import rescala.debuggable.ChromeDebuggerInterface
import rescala.restoration.LocalStorageStore
import rescala.lattices.sequences.RGOA
import scalatags.JsDom.all._
import scalatags.JsDom.tags2.section

import scala.concurrent.{Future, Promise}
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.util.Try


object Todolist {


  type TodoTransfer = RGOA[taskHandling.Taskref]

  implicit val storingEngine: LocalStorageStore = new LocalStorageStore()
  import storingEngine._

  val taskHandling = new TaskHandling
  val todoApp = TodoApp(taskHandling)

  val registry = new Registry
  val crdtDescriptions = Binding[TodoTransfer => Unit]("taskData")

  val remoteConnected = Evt[RemoteRef]

  def main(args: Array[String]): Unit = {

    ChromeDebuggerInterface.setup(storingEngine)

    val remote = Events.fromCallback[RGOA[taskHandling.Taskref]](
      cb => registry.bind(crdtDescriptions) { td: TodoTransfer =>
        println(s"receiving tasklist $td")
        Try{cb(td)}.failed.foreach{println}
        println(s"caled cb")
      })

    remote.event.observe(tl => println(s"received $tl"))

    val todores = todoApp.getContents(remote.event)

    document.body.replaceChild(todores.div.render, document.body.firstElementChild)
    document.body.appendChild(webrtchandlingArea.render)

    remoteConnected.observe { remote =>
      println(s"host connected $remote")
      val res = registry.lookup(crdtDescriptions, remote)
      todores.tasklist.observe { s =>
        println(s"sending tasklist $s")
        res(s)
      }
    }

    ChromeDebuggerInterface.finishedLoading()
  }




  def webrtchandlingArea: Tag = {

    val renderedTa = textarea().render
    val renderedPre = pre().render

    var pn: Option[PendingNonesense] = None

    val hb = button("host", onclick := { uie: UIEvent =>
      val res = webrtcIntermediate(WebRTC.offer())
      res.session.foreach(s => renderedPre.textContent = s.asJson.noSpaces)
      pn = Some(res)
      val remoteFut = registry.connect(res.connector)
      remoteFut.foreach(remoteConnected.fire(_))
    })


    val cb = button("connect", onclick := { uie: UIEvent =>
      val cs = io.circe.parser.decode[WebRTC.CompleteSession](renderedTa.value).right.get
      (pn match {
        case None     => // we are client
          val res = webrtcIntermediate(WebRTC.answer())
          res.session.foreach(s => renderedPre.textContent = s.asJson.noSpaces)
          registry.connect(res.connector).foreach(remoteConnected.fire(_))
          res.connector
        case Some(ss) => // we are server
          ss.connector
      }) set cs
    })

    section(hb, cb, renderedPre, renderedTa)
  }




  case class PendingNonesense(connector: WebRTC.Connector,
                         session: Future[WebRTC.CompleteSession])

  def webrtcIntermediate(cf: ConnectorFactory) = {
{'fraction_non_alphanumeric': 0.1059355039725814, 'fraction_numerical': 0.0003115750116840629, 'mean_word_length': 3.4926522043386985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '4212119', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1943, 'n_words': 377}