Created
August 21, 2012 16:25
-
-
Save darkfrog26/3417029 to your computer and use it in GitHub Desktop.
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
/** | |
* LiveConnections are created for each window representation of the LivePage. This allows LiveChange lists to be | |
* trimmed and only include relevant changes for the specific window instance. Reloading a page will reset the | |
* LiveConnection. | |
*/ | |
class LiveConnection(created: Long = System.currentTimeMillis(), var lastUpdated: Long = System.currentTimeMillis()) { | |
private var changes = Vector.empty[LiveChange] | |
/** | |
* Retrieves all changes since lastId or Nil if no changes. | |
*/ | |
def apply(lastId: Int): Seq[LiveChange] = synchronized { | |
if (changes.nonEmpty) { | |
val head = changes.head | |
if (head.id <= lastId) { // Client verified receipt of this id - remove and continue | |
changes = changes.tail | |
apply(lastId) | |
} else { // Client hasn't receive this id, so send the rest of the list | |
lastUpdated = System.currentTimeMillis() | |
changes | |
} | |
} else { // No changes to send | |
lastUpdated = System.currentTimeMillis() | |
Nil | |
} | |
} | |
def +=(change: LiveChange) = synchronized { | |
changes = changes :+ change | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment