From 345d38c28d94857eae30c1598e16c50b26266593 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 27 May 2016 14:39:33 +0100 Subject: [PATCH] Don't clean up a documents event listener until after the socket has disconnected on error --- .../coffee/ide/connection/ConnectionManager.coffee | 4 ++-- services/web/public/coffee/ide/editor/Document.coffee | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/services/web/public/coffee/ide/connection/ConnectionManager.coffee b/services/web/public/coffee/ide/connection/ConnectionManager.coffee index 4db3efdbe6..1c6714f59a 100644 --- a/services/web/public/coffee/ide/connection/ConnectionManager.coffee +++ b/services/web/public/coffee/ide/connection/ConnectionManager.coffee @@ -52,6 +52,7 @@ define [], () -> "force new connection": true @ide.socket.on "connect", () => + sl_console.log "[socket.io connect] Connected" @connected = true @ide.pushEvent("connected") @@ -61,8 +62,6 @@ define [], () -> if @$scope.state.loading @$scope.state.load_progress = 70 - sl_console.log "[socket.io connect] Connected" - setTimeout(() => @joinProject() , 100) @@ -75,6 +74,7 @@ define [], () -> @ide.socket.on 'disconnect', () => + sl_console.log "[socket.io disconnect] Disconnected" @connected = false @ide.pushEvent("disconnected") diff --git a/services/web/public/coffee/ide/editor/Document.coffee b/services/web/public/coffee/ide/editor/Document.coffee index 49e6822c7e..78431b8b6b 100644 --- a/services/web/public/coffee/ide/editor/Document.coffee +++ b/services/web/public/coffee/ide/editor/Document.coffee @@ -198,6 +198,7 @@ define [ @leave() _onDisconnect: () -> + sl_console.log '[onDisconnect] disconnecting' @connected = false @joined = false @doc?.updateConnectionState "disconnected" @@ -253,7 +254,7 @@ define [ else # It's possible that this instance has error, and the doc has been reloaded. # This creates a new instance in Document.openDoc with the same id. We shouldn't - # clear it because it's not use. + # clear it because it's not this instance. sl_console.log "[_cleanUp] New instance of (#{@doc_id}) created. Not removing" @_unBindFromEditorEvents() @_unBindFromSocketEvents() @@ -296,5 +297,9 @@ define [ console.error "ShareJS error", error, meta ga?('send', 'event', 'error', "shareJsError", "#{error.message} - #{@ide.socket.socket.transport.name}" ) @doc?.clearInflightAndPendingOps() - @_cleanUp() @trigger "error", error, meta + # The clean up should run after the error is triggered because the error triggers a + # disconnect. If we run the clean up first, we remove our event handlers and miss + # the disconnect event, which means we try to leaveDoc when the connection comes back. + # This could intefere with the new connection of a new instance of this document. + @_cleanUp()