Record last updated date for reconnecting

This commit is contained in:
James Allen 2014-06-24 16:56:31 +01:00
parent f727bb775d
commit 35c7f27788
4 changed files with 48 additions and 23 deletions

View file

@ -54,7 +54,7 @@ block content
include ./editor/file-tree
.ui-layout-center
#editor(ace-editor, theme="'cobalt'", show-print-margin="false", sharejs-doc="editor.sharejs_doc")
#editor(ace-editor, theme="'cobalt'", show-print-margin="false", sharejs-doc="editor.sharejs_doc", last-updated="editor.last_updated")
//- #loadingScreen
//- h3 Loading...

View file

@ -69,9 +69,17 @@ define [], () ->
@ide.socket.disconnect()
startAutoReconnectCountdown: () ->
# TODO: Reconnect slowly if no recent updates
lastUpdated = @ide.editorManager.lastUpdated()
twoMinutes = 2 * 60 * 1000
if lastUpdated? and new Date() - lastUpdated > twoMinutes
# between 1 minute and 3 minutes
countdown = 60 + Math.floor(Math.random() * 120)
else
countdown = 3 + Math.floor(Math.random() * 7)
@$scope.$apply () =>
@$scope.connection.reconnection_countdown = 3 + Math.floor(Math.random() * 7)
@$scope.connection.reconnection_countdown = countdown
setTimeout(=>
if !@connected

View file

@ -6,6 +6,7 @@ define [
constructor: (@ide, @$scope) ->
@$scope.editor = {
sharejs_doc: null
last_updated: null
}
openDoc: (doc, options = {}) ->
@ -53,3 +54,6 @@ define [
_unbindFromDocumentEvents: (document) ->
document.off()
lastUpdated: () ->
@$scope.editor.last_updated

View file

@ -17,6 +17,7 @@ define [
showPrintMargin: "="
keybindings: "="
sharejsDoc: "="
lastUpdated: "="
}
link: (scope, element, attrs) ->
editor = Ace.edit(element.find(".ace-editor-body")[0])
@ -46,34 +47,46 @@ define [
editor.setKeyboardHandler(keybindings[value])
scope.$watch "sharejsDoc", (sharejs_doc, old_sharejs_doc) ->
console.log "attaching doc to ace", sharejs_doc, old_sharejs_doc
if old_sharejs_doc?
old_sharejs_doc.detachFromAce()
old_sharejs_doc.off "remoteop.recordForUndo"
detachFromAce(old_sharejs_doc)
if sharejs_doc?
lines = sharejs_doc.getSnapshot().split("\n")
editor.setSession(new EditSession(lines))
session = editor.getSession()
session.setUseWrapMode(true)
session.setMode(new LatexMode())
attachToAce(sharejs_doc)
undoManager = new UndoManager({
showUndoConflictWarning: () ->
scope.$apply () ->
scope.undo.show_remote_warning = true
attachToAce = (sharejs_doc) ->
lines = sharejs_doc.getSnapshot().split("\n")
editor.setSession(new EditSession(lines))
session = editor.getSession()
session.setUseWrapMode(true)
session.setMode(new LatexMode())
$timeout () ->
scope.undo.show_remote_warning = false
, 4000
doc = session.getDocument()
console.log "Document", doc
doc.on "change", () ->
console.log "DOC CHANGE"
scope.$apply () ->
scope.lastUpdated = new Date()
})
session.setUndoManager(undoManager)
undoManager = new UndoManager({
showUndoConflictWarning: () ->
scope.$apply () ->
scope.undo.show_remote_warning = true
sharejs_doc.on "remoteop.recordForUndo", () =>
undoManager.nextUpdateIsRemote = true
$timeout () ->
scope.undo.show_remote_warning = false
, 4000
sharejs_doc.attachToAce(editor)
})
session.setUndoManager(undoManager)
sharejs_doc.on "remoteop.recordForUndo", () =>
undoManager.nextUpdateIsRemote = true
sharejs_doc.attachToAce(editor)
detachFromAce = (sharejs_doc) ->
old_sharejs_doc.detachFromAce()
old_sharejs_doc.off "remoteop.recordForUndo"
template: """
<div class="ace-editor-wrapper">