This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from fomkin/issue-71
Fix #71: Korolev initialization hangs
- Loading branch information
Showing
8 changed files
with
67 additions
and
22 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
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
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
25 changes: 25 additions & 0 deletions
25
integration-tests/src/main/resources/static/debug-console.js
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,25 @@ | ||
window.document.addEventListener("DOMContentLoaded", function() { | ||
Bridge.setProtocolDebugEnabled(true); | ||
var display = document.createElement("pre"); | ||
display.innerHTML = "<div><strong>Client log</strong></div>" | ||
document.body.appendChild(display); | ||
console.log = function() { | ||
var line = document.createElement("div"); | ||
for (var i = 0; i < arguments.length; i++) | ||
line.textContent = arguments[i]; | ||
display.appendChild(line); | ||
} | ||
console.error = function() { | ||
var line = document.createElement("div"); | ||
line.setAttribute("style", "color: red"); | ||
for (var i = 0; i < arguments.length; i++) | ||
line.textContent = arguments[i]; | ||
display.appendChild(line); | ||
} | ||
document.body.addEventListener('click', function(e) { | ||
console.log('! click on ' + e.target); | ||
}); | ||
window.onerror = function(e) { | ||
console.error(e); | ||
}; | ||
}); |
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
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
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
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 |
---|---|---|
@@ -1,18 +1,23 @@ | ||
package korolev.server | ||
|
||
import java.util.concurrent.ConcurrentLinkedQueue | ||
|
||
import bridge.JSAccess | ||
import korolev.Async | ||
import korolev.Async.Promise | ||
|
||
import scala.annotation.switch | ||
import scala.collection.immutable.Queue | ||
import scala.collection.concurrent.TrieMap | ||
import scala.collection.mutable | ||
import scala.language.higherKinds | ||
|
||
/** | ||
* @author Aleksey Fomkin <[email protected]> | ||
*/ | ||
case class JsonQueuedJsAccess[F[+_]: Async](sendJson: String => Unit) extends JSAccess[F] { | ||
|
||
@volatile var queue = Queue.empty[String] | ||
protected val promises = TrieMap.empty[Int, Promise[F, Any]] | ||
protected val callbacks = TrieMap.empty[String, (Any) => Unit] | ||
val queue = new ConcurrentLinkedQueue[String]() | ||
|
||
def escape(sb: StringBuilder, s: String, unicode: Boolean): Unit = { | ||
sb.append('"') | ||
|
@@ -58,18 +63,20 @@ case class JsonQueuedJsAccess[F[+_]: Async](sendJson: String => Unit) extends JS | |
*/ | ||
def send(args: Seq[Any]): Unit = { | ||
val message = seqToJSON(args) | ||
queue = queue.enqueue(message) | ||
queue.add(message) | ||
} | ||
|
||
|
||
override def flush(): Unit = { | ||
val rawRequests = synchronized { | ||
val items = queue | ||
queue = Queue.empty | ||
items | ||
val buffer = mutable.Buffer.empty[String] | ||
while (!queue.isEmpty) { | ||
buffer += queue.poll() | ||
} | ||
if (buffer.size == 1) { | ||
sendJson(buffer.head) | ||
} | ||
if (rawRequests.nonEmpty) { | ||
val requests = rawRequests.mkString(",") | ||
else if (buffer.nonEmpty) { | ||
val requests = buffer.mkString(",") | ||
sendJson(s"""["batch",$requests]""") | ||
} | ||
} | ||
|