<commit_before>package com.campudus.scycle.dom

import com.campudus.scycle.Driver
import com.campudus.scycle.dom.DomDriver.SelectedEvents
import com.campudus.scycle.vdom.VirtualDom
import com.campudus.scycle.vdom.VirtualDom.Replacement
import org.scalajs.dom._
import rxscalajs._
import rxscalajs.subscription.AnonymousSubscription

import scala.collection.mutable

class DomDriver private(val domSelector: String, selectedEvents: SelectedEvents = mutable.Map.empty)
  extends Driver[Hyperscript] {

  override def subscribe(inputs: Observable[Hyperscript]): AnonymousSubscription = {
    inputs.subscribe(hs => {
      // FIXME how to get rid of this null check caused by Scycle -> sinkProxies(name).next(null)
      if (hs != null) {
        val container = document.querySelector(domSelector)
        if (container == null) {
          console.log("NO CONTAINER FOUND WITH", domSelector)
        }
        val diff = VirtualDom.diff(VirtualDom(container), hs)
        diff match {
          case List(Replacement(_, null)) =>
          case diffs => VirtualDom.update(container, diffs)
        }

        selectedEvents.foreach({
          case ((what, eventName), (subj, subs)) =>
            org.scalajs.dom.console.log("unsubscribing", what, eventName, "of", subj.toString)
            subs.unsubscribe()
            org.scalajs.dom.console.log("unsubscribing", what, eventName, "of", subs)
            if (selectedEvents.isDefinedAt(what, eventName)) {
              console.log(s"currently selectedEvents($what, $eventName) =", selectedEvents(what, eventName).toString)
            }
            val subsNew = Observable.fromEvent(document.querySelector(what), eventName).subscribe(subj)
            org.scalajs.dom.console.log("subscribing to", what, eventName, "into", subj.toString, "again")
            org.scalajs.dom.console.log("selectedEvents got", selectedEvents.size)
            selectedEvents += (what, eventName) -> (subj, subsNew)
        })
      }
    })
  }

  def select(what: String): DomDriver = {
    new DomDriver(s"$domSelector $what", DomDriver.this.selectedEvents)
  }

  def events(what: String): Observable[Event] = {
    val subj = Subject[Event]()
    val selected = document.querySelector(domSelector)
    console.log("selecting", domSelector, selected)
    val subs = if (selected != null) {
      Observable.fromEvent(selected, what).subscribe(subj)
    } else {
      Observable.fromEvent(document.querySelector("#app"), what).subscribe(subj)
    }
    org.scalajs.dom.console.log("subscribing to", domSelector, what, subs)
    selectedEvents += (domSelector -> what) -> (subj, subs)
    subj.map(event => {
      console.log("there was an event in", domSelector, "->", what, "resulting in", event)
      event
    })
  }

}

object DomDriver {

  def makeDomDriver(domSelector: String) = new DomDriver(domSelector)

  type SelectedEvents = mutable.Map[(String, String), (Subject[Event], AnonymousSubscription)]

}
<commit_msg>Use parent selectedEvents in DomDriver explicitly<commit_after>package com.campudus.scycle.dom

import com.campudus.scycle.Driver
import com.campudus.scycle.dom.DomDriver.SelectedEvents
import com.campudus.scycle.vdom.VirtualDom
import com.campudus.scycle.vdom.VirtualDom.Replacement
import org.scalajs.dom._
import rxscalajs._
import rxscalajs.subscription.AnonymousSubscription

import scala.collection.mutable

class DomDriver private(val domSelector: String, parent: Option[DomDriver] = None)
  extends Driver[Hyperscript] {

  lazy val allSelectedEvents: SelectedEvents = {
    if (parent.isEmpty) mutable.Map.empty
    else parent.get.selectedEvents
  }

  def selectedEvents: SelectedEvents = {
    parent match {
      case None => allSelectedEvents
      case Some(p) => p.selectedEvents
    }
  }

  override def subscribe(inputs: Observable[Hyperscript]): AnonymousSubscription = {
    inputs.subscribe(hs => {
      // FIXME how to get rid of this null check caused by Scycle -> sinkProxies(name).next(null)
      if (hs != null) {
        val container = document.querySelector(domSelector)
        if (container == null) {
          console.log("NO CONTAINER FOUND WITH", domSelector)
        }
        val diff = VirtualDom.diff(VirtualDom(container), hs)
        diff match {
          case List(Replacement(_, null)) =>
          case diffs => VirtualDom.update(container, diffs)
        }

        selectedEvents.foreach({
          case ((what, eventName), (subj, subs)) =>
            org.scalajs.dom.console.log("unsubscribing", what, eventName, "of", subj.toString)
            subs.unsubscribe()
            org.scalajs.dom.console.log("unsubscribing", what, eventName, "of", subs)
            if (selectedEvents.isDefinedAt(what, eventName)) {
              console.log(s"currently selectedEvents($what, $eventName) =", selectedEvents(what, eventName).toString)
            }
            val subsNew = Observable.fromEvent(document.querySelector(what), eventName).subscribe(subj)
            org.scalajs.dom.console.log("subscribing to", what, eventName, "into", subj.toString, "again")
            org.scalajs.dom.console.log("selectedEvents got", selectedEvents.size)
            selectedEvents += (what, eventName) -> (subj, subsNew)
        })
      }
    })
  }

  def select(what: String): DomDriver = {
    new DomDriver(s"$domSelector $what", Some(this))
  }

  def events(what: String): Observable[Event] = {
    val subj = Subject[Event]()
    val selected = document.querySelector(domSelector)
    console.log("selecting", domSelector, selected)
    val subs = if (selected != null) {
      Observable.fromEvent(selected, what).subscribe(subj)
    } else {
      Observable.fromEvent(document.querySelector("#app"), what).subscribe(subj)
    }
    org.scalajs.dom.console.log("subscribing to", domSelector, what, subs)
    selectedEvents += (domSelector -> what) -> (subj, subs)
    subj.map(event => {
      console.log("there was an event in", domSelector, "->", what, "resulting in", event)
      event
    })
  }

}

object DomDriver {

  def makeDomDriver(domSelector: String) = new DomDriver(domSelector)

  type SelectedEvents = mutable.Map[(String, String), (Subject[Event], AnonymousSubscription)]

}

{'fraction_non_alphanumeric': 0.12264606470304201, 'fraction_numerical': 0.0, 'mean_word_length': 3.3515406162464987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '935901', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1836, 'n_words': 393}