<commit_before>package code.comet

import com.casualmiracles.rxlift.Components._
import com.casualmiracles.rxlift.Out
import net.liftweb.common.Full

import rx.lang.scala.{Observable, Subject}
import rx.lang.scala.subjects.BehaviorSubject

case class Message(username: String, msg: String)

object Chat {
  val send: Subject[Message] = BehaviorSubject[Message](Message("System", "Welcome"))
  val allMessages: Observable[Seq[Message]] = send.scan(Seq.empty[Message])(_ :+ _)
}

class Chat extends RxCometActor {

  override def defaultPrefix = Full("chat")

  val username = text.run(Observable.just(""))

  val msgObservable = Subject[String]()
  val msg = text.run(msgObservable)

  // a textarea whose content is obtained from Chat.messages
  def toString(msgs: Seq[Message]): String = msgs.map(m ⇒ m.username + ": " + m.msg).mkString("\n")
  val allMessages: Out[String] = textArea.run(Chat.allMessages.map(toString(_)))

  // send the user's message to everyone and blank their input field
  val messages = username.values.combineLatest(msg.values).map{ case (u, m) ⇒ Message(u, m) }
  val messageDistributor = messages.map(m ⇒ {Chat.send.onNext(m); msgObservable.onNext("")} )

  // subscribe to the distributor
  handleSubscription(messageDistributor)

  publish(allMessages, username, msg)

  def render = bind("chat", "messages" -> allMessages.ui, "username" -> username.ui,  "msg" -> msg.ui)

}<commit_msg>improved the chat example<commit_after>package code.comet

import com.casualmiracles.rxlift.Components._
import com.casualmiracles.rxlift.Out
import net.liftweb.common.Full

import rx.lang.scala.{Observable, Subject}
import rx.lang.scala.subjects.BehaviorSubject

case class Message(username: String, msg: String)

object Chat {
  val send: Subject[Message] = BehaviorSubject[Message](Message("System", "Welcome"))
  val allMessages: Observable[Seq[Message]] = send.scan(Seq.empty[Message])(_ :+ _)
}

class Chat extends RxCometActor {

  override def defaultPrefix = Full("chat")

  val username = text.run(Observable.empty)

  val msgObservable = Subject[String]()
  val msg = text.run(msgObservable)

  // a textarea whose content is obtained from Chat.messages
  def msgLine(msgs: Seq[Message]): String = msgs.map(m ⇒ m.username + ": " + m.msg).mkString("\n")
  val allMessages: Out[String] = textArea.run(Chat.allMessages.map(msgLine))

  // send the user's message to everyone and blank their input field
  val messages = username.values.combineLatest(msg.values).map{ case (u, m) ⇒ Message(u, m) }
  val messageDistributor = messages.map(m ⇒ {Chat.send.onNext(m); msgObservable.onNext("")} )

  // subscribe to the distributor
  handleSubscription(messageDistributor)

  publish(allMessages, username, msg)

  def render = bind("chat", "messages" -> allMessages.ui, "username" -> username.ui,  "msg" -> msg.ui)

}
{'fraction_non_alphanumeric': 0.13469675599435826, 'fraction_numerical': 0.0, 'mean_word_length': 4.935146443514644, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, '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': '5553666', 'n_tokens_mistral': 928, 'n_tokens_neox': 892, 'n_words': 223}