Skip to content

Commit

Permalink
Send basic JS info in data- attributes.
Browse files Browse the repository at this point in the history
This includes the page version, session id, and comet ids and
versions. The data- attributes are set and read on the body
element of the page.
  • Loading branch information
Shadowfiend committed Apr 23, 2014
1 parent 1221e65 commit 89450a4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
41 changes: 38 additions & 3 deletions web/webkit/src/main/resources/toserve/lift.js
Expand Up @@ -342,24 +342,59 @@
}

// Call on page load
function registerComet(tw, sessId) {
function registerComet(cometGuid, cometVersion, startComet) {
if (typeof startComet == 'undefined') {
startComet = true;
}

toWatch = tw;
sessionId = sessId;

cometSuccessFunc();
if (startComet === true) {
cometSuccessFunc();
}
}

// public object
return {
init: function(options) {
// override default settings
this.extend(settings, options);

var lift = this;
$(document).ready(function() {
var gc = document.body.getAttribute('data-lift-gc');
if (gc) {
lift.startGc();
}

var attributes = document.body.attributes,
cometGuid, cometVersion;
for (var i = 0; i < attributes.length; ++i) {
if (attributes[i].name == 'data-lift-gc') {
pageId = attributes[i].value;
lift.startGc();
} else if (attributes[i].name.match(/^data-lift-comet-/)) {
cometGuid = attributes[i].name.substring('data-lift-comet-'.length);
cometVersion = parseInt(attributes[i].value)

registerComet(cometGuid, cometVersion, false);
} else if (attributes[i].name == 'data-lift-session-id') {
sessionId = attributes[i].value
}
}

if (typeof cometGuid != 'undefined') {
cometSuccessFunc(); // we saw a comet, so start the comet cycle
}
});

// start the cycle
doCycleIn200();
},
logError: settings.logError,
ajax: appendToQueue,
register: successRegisterGC,
startGc: successRegisterGC,
ajaxOnSessionLost: function() {
settings.ajaxOnSessionLost();
},
Expand Down
39 changes: 25 additions & 14 deletions web/webkit/src/main/scala/net/liftweb/http/LiftMerge.scala
Expand Up @@ -203,19 +203,6 @@ private[http] trait LiftMerge {
S.appendJs(LiftJavaScript.initCmd(LiftRules.javascriptSettings.vend().apply(this)))
}

if (!cometList.isEmpty && LiftRules.autoIncludeComet(this)) {
S.appendJs(LiftRules.renderCometPageContents(this, cometList))
}

if (LiftRules.enableLiftGC && stateful_?) {
import js.JsCmds._

S.appendJs(JE.Call("window.lift.setPageId", RenderVersion.get))

if (!cometList.isEmpty || hasFuncsForOwner(RenderVersion.get))
S.appendJs(JE.Call("window.lift.register"))
}

S.jsToAppend match {
case Nil =>
case x :: Nil => addlTail += js.JsCmds.Script(x)
Expand All @@ -228,10 +215,34 @@ private[http] trait LiftMerge {

bodyChildren += nl

val bodyAttributes: List[(String, Option[String])] =
("data-lift-gc" ->
Some(RenderVersion.get).filter { _ =>
LiftRules.enableLiftGC && stateful_?
}
) ::
(
if (LiftRules.autoIncludeComet(this)) {
("data-lift-session-id" -> Some(S.session.map(_.uniqueId) openOr "xx")) ::
cometList.map {
case CometVersionPair(guid, version) =>
(s"data-lift-comet-$guid" -> Some(version.toString))
}
} else {
Nil
}
)

htmlKids += nl
htmlKids += Elem(headTag.prefix, headTag.label, headTag.attributes, headTag.scope, headTag.minimizeEmpty, headChildren.toList: _*)
htmlKids += nl
htmlKids += Elem(bodyTag.prefix, bodyTag.label, bodyTag.attributes, bodyTag.scope, bodyTag.minimizeEmpty, bodyChildren.toList: _*)
htmlKids +=
bodyAttributes.foldLeft(bodyTag.copy(child = bodyChildren.toList)) { (element, attribute) =>
attribute match {
case (name, Some(value)) => element % (name -> value)
case _ => element
}
}
htmlKids += nl

val tmpRet = Elem(htmlTag.prefix, htmlTag.label, htmlTag.attributes, htmlTag.scope, htmlTag.minimizeEmpty, htmlKids.toList: _*)
Expand Down
13 changes: 5 additions & 8 deletions web/webkit/src/main/scala/net/liftweb/http/LiftRules.scala
Expand Up @@ -1684,14 +1684,6 @@ class LiftRules() extends Factory with FormVendor with LazyLoggable {
Empty
} ) {}

/**
* Renders that JavaScript that holds Comet identification information
*/
@volatile var renderCometPageContents: (LiftSession, Seq[CometVersionPair]) => JsCmd =
(session, vp) => JsCmds.Run(
"window.lift.registerComet(" + vp.map(p => p.guid.encJs + ": " + p.version).mkString("{", " , ", "}") + ", '"+S.encodeURL(session.uniqueId)+"');"
)

/**
* Holds the last update time of the Ajax request. Based on this server may return HTTP 304 status
* indicating the client to used the cached information.
Expand Down Expand Up @@ -2073,6 +2065,11 @@ trait CometVersionPair {

def version: Long
}
object CometVersionPair {
def unapply(pair: CometVersionPair): Option[(String, Long)] = {
Some((pair.guid, pair.version))
}
}

case class CVP(guid: String, version: Long) extends CometVersionPair

Expand Down

0 comments on commit 89450a4

Please sign in to comment.