-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.js
192 lines (167 loc) · 5.54 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*globals g_oEditSessions*/
var oHelpers = require('./helpers-node');
var EditSession = require('./edit-session');
var Document = require('./document');
var oDatabase = require('./database');
var a_PREVIEW_ACTION_TYPES = ['setDocumentData', 'docChange', 'setDocumentTitle', 'error', 'setAutoRefreshPreview', 'refreshPreview'];
module.exports = oHelpers.createClass(
{
_oSocket: null,
_oEditSession: null,
_bCreatedDocument: false,
_bIsPreview: false,
_aPreInitActionQueue: null,
_bInitialized: false,
_bClosed: false,
_sClientID: '',
_oLastSelRange: null,
__init__: function(oSocket)
{
// Init objects.
this._aPreInitActionQueue = [];
this._oSocket = oSocket;
// Initial selection at start of document.
this._oLastSelRange = (
{
oStart: {iRow: 0, iCol: 0},
oEnd: {iRow: 0, iCol: 0}
});
// Attach socket events.
oSocket.on('message', oHelpers.createCallback(this, this._onClientAction));
oSocket.on('close', oHelpers.createCallback(this, this._onSocketClose));
},
setClientID: function(sClientID)
{
this._sClientID = sClientID;
},
getClientID: function()
{
oHelpers.assert(this._sClientID, 'The client ID is not yet initialized.');
return this._sClientID;
},
getSelectionRange: function()
{
return this._oLastSelRange;
},
setSelectionRange: function(oRange)
{
this._oLastSelRange = oRange;
},
createdDocument: function()
{
return this._bCreatedDocument;
},
isPreview: function()
{
return this._bIsPreview;
},
onDocumentLoad: function()
{
// Send queued actions.
this._bInitialized = true;
while (this._aPreInitActionQueue.length)
{
this._onClientAction(this._aPreInitActionQueue.pop());
}
},
sendAction: function(param1, param2) /* either sendAction(sType, oData) or sendAction(oAction)*/
{
var oAction;
if (typeof(param1) === 'string')
{
oAction = (
{
sType: param1,
oData: param2
});
}
else
{
oHelpers.assert(typeof(param1) === 'object', 'Invalid parameter type');
oAction = param1;
}
// Only send relevent events to a preview client.
if (!this._bIsPreview || oHelpers.inArray(oAction.sType, a_PREVIEW_ACTION_TYPES))
this._oSocket.send(oHelpers.toJSON(oAction), oHelpers.createCallback(this, this._onSocketError));
},
abort: function(sMessage)
{
this.sendAction('error', {'sMessage': sMessage});
this._oSocket.close();
},
_onClientAction: function(sJSONAction)
{
var oAction = oHelpers.fromJSON(sJSONAction);
switch(oAction.sType)
{
case 'createDocument':
this._bCreatedDocument = true;
var oNewDocument = new Document(oAction.oData);
oDatabase.createDocument(oNewDocument.toJSON(), this, function(sDocumentID)
{
this._addToEditSession(sDocumentID);
});
break;
case 'openDocument':
this._bIsPreview = oAction.oData.bIsPreview || false;
this._addToEditSession(oAction.oData.sDocumentID);
break;
case 'close':
this._closeSocket();
break;
default:
if (this._bInitialized )
this._oEditSession.onClientAction(this, oAction);
else
this._aPreInitActionQueue.push(sJSONAction);
}
},
_addToEditSession: function(sDocumentID)
{
// Validate.
oHelpers.assert(!this._oEditSession, 'Client already connected.');
if (this._bClosed)
return;
// Get or add workspace.
if (sDocumentID in g_oEditSessions)
{
this._oEditSession = g_oEditSessions[sDocumentID];
this._oEditSession.addClient(this);
}
else
{
// TODO (AldenD 06-29-2013): On document creation we could tell the workspace
// not to go to the database and directly give it the mode.
this._oEditSession = new EditSession(sDocumentID, this);
}
},
_closeSocket: function()
{
// the .close() socket method does not trigger the "close" event.
// as a result, we have to manually all the close handler.
this._oSocket.close();
this._onSocketClose();
},
_onSocketClose: function()
{
// WARNING: In IE, a closed socket sometimes gets resurrected by magic
// when the user navigates away from the page via the back button and then
// returns with the forward button. To handle this, we need to blank out
// the edit session data so the client gets correctly added back to the
// edit session.
if (this._oEditSession)
{
this._oEditSession.removeClient(this);
this._oEditSession = null;
}
this._bClosed = true;
},
_onSocketError: function(oError)
{
if (oError)
{
console.log('Socket Error: ', oError.message);
this._closeSocket();
}
}
});