<commit_before>package net.liftmodules.ng

import net.liftweb._
import http._
import common._
import util._
import js._
import JE._
import JsCmds._
import StringHelpers._
import json.Serialization._
import json.DefaultFormats

/** A comet actor for Angular action */
trait AngularActor extends CometActor with Loggable {
  private def rand = "NG"+randomString(18)
  private val id:String = rand

  /** Render a div for us to hook into */
  def render = <div id={id}></div>

  private implicit val formats = DefaultFormats // Some crap needed for stringify
  protected def stringify(obj:AnyRef):String = obj match {
      case s:String => "'"+s+"'"
      case _ => write(obj)
    }

  trait Scope {
    // TODO: Use an Int and change this to obj:Any??
    /** Performs a <code>\$broadcast()</code> with the given event name and object argument */
    def broadcast(event:String, obj:AnyRef):Unit = partialUpdate(eventCmd(scopeVar, "broadcast", event, obj))
    /** Performs a <code>\$emit()</code> with the given event name and object argument */
    def emit(event:String, obj:AnyRef):Unit = partialUpdate(eventCmd(scopeVar, "emit", event, obj))
    /** Performs assigns the second argument to the scope variable/field specified in the first argument */
    def assign(field:String, obj:AnyRef):Unit = partialUpdate(assignCmd(scopeVar, field, obj))

    /** Variables needed to perform any of our angular actions (will be \$scope and possibly \$rootScope) */
    protected def vars:String
    /** The variable name of the scope variable for this scope (either \$scope or \$rootScope) */
    protected def scopeVar:String

    /** Variable assignment for \$scope */
    protected val varScope = "var s=angular.element(document.querySelector('#"+id+"')).scope();"
    /** Variable assignment for \$rootScope */
    protected val varRoot  = "var r=(typeof s==='undefined')?void 0:s.$root;"

    private def eventCmd(scopeVar:String, method:String, event:String, obj:AnyRef):JsCmd = {
      val ready = "var t=function(){return typeof " + scopeVar + "!=='undefined';};"
      val fn = "var f=function(){"+scopeVar+".$apply(function(){"+scopeVar+".$"+method+"('"+event+"',"+stringify(obj)+");});};"
      val dequeue = "var d=function(){" +
        "if(q[0].t()){"+
          "for(i=0;i<q.length;i++){" +
            "q[i].f();"+
          "}"+
          "q=void 0;"+
        "}else{"+
          "setTimeout(function(){d();},500);"+
        "}"+
      "};"
      val enqueue = "if(typeof q==='undefined'){q=[];setTimeout(function(){d();},500);}" +
        "q.push({t:t,f:f});"
      JsRaw(vars+ready+fn+dequeue+"if(typeof q==='undefined'&&t()){f();}else{"+enqueue+"}")
    }

    private def assignCmd(scopeVar:String, field:String, obj:AnyRef):JsCmd = {
      val assignment = scopeVar+".$apply(function(){"+scopeVar+"."+field+"="+stringify(obj)+";});"
      JsRaw(vars+assignment)
    }
  }

  private class ChildScope extends Scope {
    override val vars = varScope
    override val scopeVar = "s"
  }

  /** Your handle to the \$scope object for your actor */
  val scope:Scope = new ChildScope

  /** Your handle to the \$rootScope object for your actor */
  object rootScope extends Scope {
    override val vars = varScope+varRoot
    override val scopeVar = "r"
  }

}<commit_msg>Gave a full name to the global var we use for queuing messages, and updated assign to use the same logic<commit_after>package net.liftmodules.ng

import net.liftweb._
import http._
import common._
import util._
import js._
import JE._
import JsCmds._
import StringHelpers._
import json.Serialization._
import json.DefaultFormats

/** A comet actor for Angular action */
trait AngularActor extends CometActor with Loggable {
  private def rand = "NG"+randomString(18)
  private val id:String = rand

  /** Render a div for us to hook into */
  def render = <div id={id}></div>

  private implicit val formats = DefaultFormats // Some crap needed for stringify
  protected def stringify(obj:AnyRef):String = obj match {
      case s:String => "'"+s+"'"
      case _ => write(obj)
    }

  trait Scope {
    // TODO: Use an Int and change this to obj:Any??
    /** Performs a <code>\$broadcast()</code> with the given event name and object argument */
    def broadcast(event:String, obj:AnyRef):Unit = partialUpdate(eventCmd("broadcast", event, obj))
    /** Performs a <code>\$emit()</code> with the given event name and object argument */
    def emit(event:String, obj:AnyRef):Unit = partialUpdate(eventCmd("emit", event, obj))
    /** Performs assigns the second argument to the scope variable/field specified in the first argument */
    def assign(field:String, obj:AnyRef):Unit = partialUpdate(assignCmd(field, obj))

    /** Variables needed to perform any of our angular actions (will be \$scope and possibly \$rootScope) */
    protected def vars:String
    /** The variable name of the scope variable for this scope (either \$scope or \$rootScope) */
    protected def scopeVar:String

    /** Variable assignment for \$scope */
    protected val varScope = "var s=angular.element(document.querySelector('#"+id+"')).scope();"
    /** Variable assignment for \$rootScope */
    protected val varRoot  = "var r=(typeof s==='undefined')?void 0:s.$root;"

    private def eventCmd(method:String, event:String, obj:AnyRef):JsCmd = {
      doCmd(scopeVar+".$apply(function(){"+scopeVar+".$"+method+"('"+event+"',"+stringify(obj)+");});")
    }

    private def assignCmd(field:String, obj:AnyRef):JsCmd = {
      doCmd(scopeVar+".$apply(function(){"+scopeVar+"."+field+"="+stringify(obj)+";});")
    }

    private def doCmd(f:String):JsCmd = {
      val ready = "var t=function(){return typeof " + scopeVar + "!=='undefined';};"
      val fn = "var f=function(){"+f+"};"
      val dequeue = "var d=function(){" +
        "if(net_liftmodules_ng_q[0].t()){"+
          "for(i=0;i<net_liftmodules_ng_q.length;i++){" +
            "net_liftmodules_ng_q[i].f();"+
          "}"+
          "net_liftmodules_ng_q=void 0;"+
        "}else{"+
          "setTimeout(function(){d();},500);"+
        "}"+
      "};"
      val enqueue = "if(typeof net_liftmodules_ng_q==='undefined'){net_liftmodules_ng_q=[];setTimeout(function(){d();},500);}" +
        "net_liftmodules_ng_q.push({t:t,f:f});"
      JsRaw(vars+ready+fn+dequeue+"if(typeof net_liftmodules_ng_q==='undefined'&&t()){f();}else{"+enqueue+"}")
    }
  }

  private class ChildScope extends Scope {
    override val vars = varScope
    override val scopeVar = "s"
  }

  /** Your handle to the \$scope object for your actor */
  val scope:Scope = new ChildScope

  /** Your handle to the \$rootScope object for your actor */
  object rootScope extends Scope {
    override val vars = varScope+varRoot
    override val scopeVar = "r"
  }

}
{'fraction_non_alphanumeric': 0.15763837638376385, 'fraction_numerical': 0.0035424354243542436, 'mean_word_length': 3.6925207756232687, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2136824', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2041, 'n_words': 590}