Remove dependency on a global socket and address `pad.socket` instead.

pull/344/head
Chad Weider 2012-01-17 00:43:11 -08:00
parent 6bbc32a19f
commit 003c2a59aa
2 changed files with 16 additions and 8 deletions

View File

@ -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",

View File

@ -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);