Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created August 21, 2012 16:25
Show Gist options
  • Save darkfrog26/3417029 to your computer and use it in GitHub Desktop.
Save darkfrog26/3417029 to your computer and use it in GitHub Desktop.
/**
* 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