-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package controllers | ||
|
||
import play.api.templates.Html | ||
import scala.collection.mutable.Map | ||
import rx.lang.scala.Observable | ||
import rx.lang.scala._ | ||
|
||
import ImplicitFunctionConversions._ | ||
import JavaConversions._ | ||
|
||
object CometObs { | ||
|
||
/** | ||
* Transform an Observable into a Comet Observable. | ||
* | ||
* @tparam E Type of messages handled by this comet stream. | ||
* @param callback Javascript function to call on the browser for each message. | ||
* @param initialChunk Initial chunk of data to send for browser compatibility (default to send 5Kb of blank data) | ||
*/ | ||
|
||
def apply[E](obs: Observable[E], callback: String, initialChunk: Html = Html(Array.fill[Char](5 * 1024)(' ').mkString + "<html><body>")): Observable[Html] = { | ||
|
||
val obsNext: Observable[Html] = convertToJsFunction(obs, callback) | ||
|
||
if (initialChunk != null) { | ||
val obsInit: Observable[Html] = Observable.from(List(initialChunk)) | ||
obsInit ++ obsNext | ||
} else { | ||
obsNext | ||
} | ||
} | ||
|
||
def apply[E](obsMap: Map[String, Observable[E]], initialChunk: Html = Html(Array.fill[Char](5 * 1024)(' ').mkString + "<html><body>")): Observable[Html] = { | ||
|
||
val obsInit: Observable[Html] = Observable.from(List(initialChunk)) | ||
|
||
val htmlObsList = obsMap.map(el => convertToJsFunction(el._2, el._1)) | ||
|
||
// merging all observables from the list of callback functions with corresponding values to a single observable of everything | ||
obsInit ++ htmlObsList.reduceLeft((a, b) => a.mergeDelayError(b)) | ||
} | ||
|
||
private def convertToJsFunction[E](obs: Observable[E], callback: String): Observable[Html] = { | ||
obs.map[Html](data => Html("<script type=\"text/javascript\">" + callback + "(\"" + escapeString(data.toString) + "\");</script>")) | ||
} | ||
|
||
private def escapeString(str : String) = str.replace("\\","\\\\").replace("\n","\\n").replace("\b","\\b").replace("\r","\\r").replace("\t","\\t").replace("\'","\\'").replace("\f","\\f").replace("\"","\\\"") | ||
} |