Skip to content

Commit

Permalink
WIP revamping Ajax calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dpp committed Apr 28, 2011
1 parent 0e17bce commit 769bef0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 63 deletions.
7 changes: 7 additions & 0 deletions web/webkit/src/main/scala/net/liftweb/http/LiftMerge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ private[http] trait LiftMerge {

bodyChildren += JsCmds.Script((if (!cometList.isEmpty || hasFuncsForOwner(RenderVersion.get)) OnLoad(JsRaw("liftAjax.lift_successRegisterGC()")) else Noop) &
JsCrVar("lift_page", RenderVersion.get))
} else if (stateful_?) {
import js._
import JsCmds._
import JE._

// always include the lift_page
bodyChildren += JsCrVar("lift_page", RenderVersion.get))
}

htmlKids += nl
Expand Down
123 changes: 67 additions & 56 deletions web/webkit/src/main/scala/net/liftweb/http/LiftServlet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -399,74 +399,85 @@ class LiftServlet extends Loggable {
toReturn
}

private def extractVersion(path: List[String]) {
private def extractVersion[T](liftSession: LiftSession,
path: List[String])(f: => T): T = {
path match {
case first :: second :: _ => RenderVersion.set(second)
case _ =>
case _ :: second :: guid :: _ if second != "na" &&
guid != "index" && guid.length > 0 =>
// recover page, lock page, set up requestvars
// de-dup GUID
RenderVersion.doWith(second)(f)

case _ :: _ :: guid :: _ if guid.length > 0 && guid != "index" =>
// dedup GUID
f

case _ => f
}
}

private def handleAjax(liftSession: LiftSession,
requestState: Req): Box[LiftResponse] =
{
extractVersion(requestState.path.partPath)

LiftRules.cometLogger.debug("AJAX Request: " + liftSession.uniqueId + " " + requestState.params)
tryo {LiftSession.onBeginServicing.foreach(_(liftSession, requestState))}

val ret = try {
requestState.param("__lift__GC") match {
case Full(_) =>
liftSession.updateFuncByOwner(RenderVersion.get, millis)
Full(JavaScriptResponse(js.JsCmds.Noop))

case _ =>
try {
val what = flatten(try {
liftSession.runParams(requestState)
} catch {
case ResponseShortcutException(_, Full(to), _) =>
import js.JsCmds._
List(RedirectTo(to))
})

val what2 = what.flatMap {
case js: JsCmd => List(js)
case n: NodeSeq => List(n)
case js: JsCommands => List(js)
case r: LiftResponse => List(r)
case s => Nil
extractVersion(liftSession, requestState.path.partPath){
LiftRules.cometLogger.debug("AJAX Request: " + liftSession.uniqueId + " " + requestState.params)
tryo {LiftSession.onBeginServicing.foreach(_(liftSession, requestState))}

val ret = try {
requestState.param("__lift__GC") match {
case Full(_) => {
liftSession.updateFuncByOwner(RenderVersion.get, millis)
Full(JavaScriptResponse(js.JsCmds.Noop))
}

val ret: LiftResponse = what2 match {
case (json: JsObj) :: Nil => JsonResponse(json)
case (js: JsCmd) :: xs => {
(JsCommands(S.noticesToJsCmd :: Nil) &
((js :: xs).flatMap
{case js: JsCmd => List(js)
case _ => Nil}.reverse) &
S.jsToAppend).toResponse
}
case _ =>
try {
val what = flatten(try {
liftSession.runParams(requestState)
} catch {
case ResponseShortcutException(_, Full(to), _) =>
import js.JsCmds._
List(RedirectTo(to))
})

case (n: Node) :: _ => XmlResponse(n)
case (ns: NodeSeq) :: _ => XmlResponse(Group(ns))
case (r: LiftResponse) :: _ => r
case _ => JsCommands(S.noticesToJsCmd :: JsCmds.Noop :: S.jsToAppend).toResponse
}

LiftRules.cometLogger.debug("AJAX Response: " + liftSession.uniqueId + " " + ret)

Full(ret)
} finally {
liftSession.updateFunctionMap(S.functionMap, RenderVersion.get, millis)
val what2 = what.flatMap {
case js: JsCmd => List(js)
case n: NodeSeq => List(n)
case js: JsCommands => List(js)
case r: LiftResponse => List(r)
case s => Nil
}

val ret: LiftResponse = what2 match {
case (json: JsObj) :: Nil => JsonResponse(json)
case (js: JsCmd) :: xs => {
(JsCommands(S.noticesToJsCmd :: Nil) &
((js :: xs).flatMap
{case js: JsCmd => List(js)
case _ => Nil}.reverse) &
S.jsToAppend).toResponse
}

case (n: Node) :: _ => XmlResponse(n)
case (ns: NodeSeq) :: _ => XmlResponse(Group(ns))
case (r: LiftResponse) :: _ => r
case _ => JsCommands(S.noticesToJsCmd :: JsCmds.Noop :: S.jsToAppend).toResponse
}

LiftRules.cometLogger.debug("AJAX Response: " + liftSession.uniqueId + " " + ret)

Full(ret)
} finally {
liftSession.updateFunctionMap(S.functionMap, RenderVersion.get, millis)
}
}
} catch {
case foc: LiftFlowOfControlException => throw foc
case e => NamedPF.applyBox((Props.mode, requestState, e), LiftRules.exceptionHandler.toList);
}
} catch {
case foc: LiftFlowOfControlException => throw foc
case e => NamedPF.applyBox((Props.mode, requestState, e), LiftRules.exceptionHandler.toList);
tryo {LiftSession.onEndServicing.foreach(_(liftSession, requestState, ret))}
ret
}
tryo {LiftSession.onEndServicing.foreach(_(liftSession, requestState, ret))}
ret
}

/**
Expand Down
10 changes: 10 additions & 0 deletions web/webkit/src/main/scala/net/liftweb/http/LiftSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,13 @@ private final case class PostPageFunctions(renderVersion: String,

}

private final case class PageInfo(lastSeen: Long,
restore: Function1[Function0[_], _],
guid: String,
sync: Object,
respMap: Map[String, Box[LiftResponse]])


/**
* The LiftSession class containg the session state information
*/
Expand Down Expand Up @@ -496,6 +503,9 @@ class LiftSession(private[http] val _contextPath: String, val uniqueId: String,
def ? = this.box openOr false
}

private var pages: Map[String, PageInfo] = Map()
private val pagesSyncObj = new Object()

/**
* ****IMPORTANT**** when you access messageCallback, it *MUST*
* be in a block that's synchronized on the owner LiftSession
Expand Down
24 changes: 17 additions & 7 deletions web/webkit/src/main/scala/net/liftweb/http/js/ScriptRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object ScriptRenderer {
toSend.onSuccess = theSuccess;
toSend.onFailure = theFailure;
toSend.responseType = responseType;
toSend.guid = Math.floor(Math.random()*10000000).toString()+Math.floor(Math.random()*10000000).toString()+Math.floor(Math.random()*10000000).toString();
liftAjax.lift_ajaxQueue.push(toSend);
liftAjax.lift_ajaxQueueSort();
Expand Down Expand Up @@ -144,6 +145,9 @@ object ScriptRenderer {
liftAjax.lift_doAjaxCycle();
};
var oldGUID = currentGUID;
try {
currentGUID = aboutToSend.guid;
if (aboutToSend.responseType != undefined &&
aboutToSend.responseType != null &&
aboutToSend.responseType.toLowerCase() === "json") {
Expand All @@ -156,21 +160,27 @@ object ScriptRenderer {
}
liftAjax.lift_actualAjaxCall(theData, successFunc, failureFunc);
}
} finally {
liftAjax.currentGUID = oldGUID;
}
}
}
liftAjax.lift_testAndShowAjax();
setTimeout("liftAjax.lift_doAjaxCycle();", 200);
},
currentGUID: "",
calcGUID: function() {
if (liftAjax.currentGUID == "") return "na";
return liftAjax.currentGUID;
}
addPageName: function(url) {
return """ + {
if (LiftRules.enableLiftGC) {
"url.replace('" + LiftRules.ajaxPath + "', '" + LiftRules.ajaxPath + "/'+lift_page);"
} else {
"url;"
}
} + """
return url + '/' + """+
(if (LiftRules.enableLiftGC) "lift_page" else "'na'")+
""" + '/' + liftAjax.calcGUID();
},
lift_actualAjaxCall: function(data, onSuccess, onFailure) {
Expand Down

0 comments on commit 769bef0

Please sign in to comment.