diff --git a/static/js/collab_client.js b/static/js/collab_client.js index fafe0fd2f..bb2351fc3 100644 --- a/static/js/collab_client.js +++ b/static/js/collab_client.js @@ -25,12 +25,20 @@ $(window).bind("load", function() getCollabClient.windowLoaded = true; }); +// Dependency fill on init. This exists for `pad.socket` only. +// TODO: bind directly to the socket. +var pad = undefined; +function getSocket() { + return pad && pad.socket; +} + /** Call this when the document is ready, and a new Ace2Editor() has been created and inited. ACE's ready callback does not need to have fired yet. "serverVars" are from calling doc.getCollabClientVars() on the server. */ -function getCollabClient(ace2editor, serverVars, initialUserInfo, options) +function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) { var editor = ace2editor; + pad = _pad; // Inject pad to avoid a circular dependency. var rev = serverVars.rev; var padId = serverVars.padId; @@ -81,7 +89,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) $(window).bind("unload", function() { - if (socket) + if (getSocket()) { setChannelState("DISCONNECTED", "unload"); } @@ -111,7 +119,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) function handleUserChanges() { - if ((!socket) || channelState == "CONNECTING") + if ((!getSocket()) || channelState == "CONNECTING") { if (channelState == "CONNECTING" && (((+new Date()) - initialStartConnectTime) > 20000)) { @@ -295,7 +303,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) function sendMessage(msg) { - socket.json.send( + getSocket().json.send( { type: "COLLABROOM", component: "pad", @@ -337,7 +345,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) { if (window.console) console.log(evt); - if (!socket) return; + if (!getSocket()) return; if (!evt.data) return; var wrapper = evt; if (wrapper.type != "COLLABROOM") return; @@ -442,7 +450,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options) userInfo.userId = userId; userSet[userId] = userInfo; tellAceActiveAuthorInfo(userInfo); - if (!socket) return; + if (!getSocket()) return; sendMessage( { type: "USERINFO_UPDATE", diff --git a/static/js/pad2.js b/static/js/pad2.js index ca4ef53fc..5872b7a11 100644 --- a/static/js/pad2.js +++ b/static/js/pad2.js @@ -185,7 +185,7 @@ function handshake() //find out in which subfolder we are var resource = loc.pathname.substr(1, loc.pathname.indexOf("/p/")) + "socket.io"; //connect - socket = io.connect(url, { + socket = pad.socket = io.connect(url, { resource: resource, 'max reconnection attempts': 3 }); @@ -474,7 +474,7 @@ var pad = { pad.collabClient = getCollabClient(padeditor.ace, clientVars.collab_client_vars, pad.myUserInfo, { colorPalette: pad.getColorPalette() - }); + }, pad); pad.collabClient.setOnUserJoin(pad.handleUserJoin); pad.collabClient.setOnUpdateUserInfo(pad.handleUserUpdate); pad.collabClient.setOnUserLeave(pad.handleUserLeave);