Buffer updates when only a single user is editing a document

Add in 5 second delay between flushing updates when only a single user
is editing a document. As soon as an update is received from another user
we switch to sending updates immediately again so there is no latency
between collaborators. The logic applies to individual docs, so two users
can be editing different docs and will still buffer updates since they
will not affect each other.
This commit is contained in:
James Allen 2015-04-17 11:22:26 +01:00
parent c583903e04
commit af85c83877
6 changed files with 28 additions and 2 deletions

View file

@ -29,7 +29,7 @@ block content
strong #{translate("reconnecting")}...
.div(ng-controller="SavingNotificationController")
.alert.alert-warning.small( ng-repeat="(doc_id, state) in docSavingStatus" ng-if="state.unsavedSeconds > 3") #{translate("saving_notification_with_seconds", {docname:"{{ state.doc.name }}", seconds:"{{ state.unsavedSeconds }}"})}
.alert.alert-warning.small( ng-repeat="(doc_id, state) in docSavingStatus" ng-if="state.unsavedSeconds > 8") #{translate("saving_notification_with_seconds", {docname:"{{ state.doc.name }}", seconds:"{{ state.unsavedSeconds }}"})}
include ./editor/left-menu

View file

@ -14,6 +14,10 @@ define [
return true if doc.hasBufferedOps()
return false
@flushAll: () ->
for doc_id, doc of @openDocs
doc.flush()
constructor: (@ide, @doc_id) ->
@connected = @ide.socket.socket.connected
@joined = false
@ -109,6 +113,9 @@ define [
else
@_leaveDoc(callback)
flush: () ->
@doc?.flushPendingOps()
pollSavedStatus: () ->
# returns false if doc has ops waiting to be acknowledged or
# sent that haven't changed since the last time we checked.

View file

@ -28,6 +28,9 @@ define [
initialized = true
@autoOpenDoc()
@$scope.$on "flush-changes", () =>
Document.flushAll()
autoOpenDoc: () ->
open_doc_id =
@ide.localStorage("doc.open_id.#{@$scope.project_id}") or

View file

@ -2,6 +2,8 @@ define [
"utils/EventEmitter"
"libs/sharejs"
], (EventEmitter, ShareJs) ->
SINGLE_USER_FLUSH_DELAY = 5000 #ms
class ShareJsDoc extends EventEmitter
constructor: (@doc_id, docLines, version, @socket) ->
# Dencode any binary bits of data
@ -33,11 +35,15 @@ define [
@_doc = new ShareJs.Doc @connection, @doc_id,
type: @type
@_doc.setFlushDelay(SINGLE_USER_FLUSH_DELAY)
@_doc.on "change", () =>
@trigger "change"
@_doc.on "acknowledge", () =>
@trigger "acknowledge"
@_doc.on "remoteop", () =>
# As soon as we're working with a collaborator, start sending
# ops as quickly as possible for low latency.
@_doc.setFlushDelay(0)
@trigger "remoteop"
@_bindToDocChanges(@_doc)

View file

@ -247,6 +247,9 @@ class Doc
# Only one op can be in-flight at a time, so if an op is already on its way then
# this method does nothing.
flush: =>
delete @flushTimeout
#console.log "CALLED FLUSH"
return unless @connection.state == 'ok' and @inflightOp == null and @pendingOp != null
# Rotate null -> pending -> inflight
@ -256,6 +259,7 @@ class Doc
@pendingOp = null
@pendingCallbacks = []
#console.log "SENDING OP TO SERVER", @inflightOp, @version
@connection.send {doc:@name, op:@inflightOp, v:@version}
# Submit an op to the server. The op maybe held for a little while before being sent, as only one
@ -277,7 +281,11 @@ class Doc
# A timeout is used so if the user sends multiple ops at the same time, they'll be composed
# & sent together.
setTimeout @flush, 0
if !@flushTimeout?
@flushTimeout = setTimeout @flush, @_flushDelay || 0
setFlushDelay: (delay) =>
@_flushDelay = delay
shout: (msg) =>
# Meta ops don't have to queue, they can go direct. Good/bad idea?

View file

@ -123,6 +123,8 @@ define [
return if $scope.pdf.compiling
$scope.pdf.compiling = true
ide.$scope.$broadcast("flush-changes")
if !options.isAutoCompile
compileCount++
if compileCount == 1