diff --git a/lib/editor-socketio-server.js b/lib/editor-socketio-server.js index 5cb8d82..0322258 100644 --- a/lib/editor-socketio-server.js +++ b/lib/editor-socketio-server.js @@ -54,91 +54,90 @@ EditorSocketIOServer.prototype.debugLog = function (msg) { EditorSocketIOServer.prototype.addClient = function (socket) { var self = this; - socket - .join(this.docId) - .emit('doc', { - str: this.document, - revision: this.operations.length, - clients: this.users - }) - .on('operation', function (revision, operation, selection, data) { - self.isBusy = true; - socket.origin = 'operation'; - if (data) { - try { - self.debugLog("new operation event: " + JSON.stringify(data)); - if (typeof self.operationEventCallback === 'function') { - self.operationEventCallback(socket, data, function () { - self.isBusy = false; - }); - } else { + socket.join(this.docId); + socket.emit('doc', { + str: this.document, + revision: this.operations.length, + clients: this.users + }); + socket.on('operation', function (revision, operation, selection, data) { + self.isBusy = true; + socket.origin = 'operation'; + if (data) { + try { + self.debugLog("new operation event: " + JSON.stringify(data)); + if (typeof self.operationEventCallback === 'function') { + self.operationEventCallback(socket, data, function () { self.isBusy = false; - } - } catch (err) { - self.logger.error(err); + }); + } else { self.isBusy = false; } + } catch (err) { + self.logger.error(err); + self.isBusy = false; + } + return; + } + self.mayWrite(socket, function (mayWrite) { + if (!mayWrite) { + self.debugLog("User doesn't have the right to edit."); + self.isBusy = false; return; } - self.mayWrite(socket, function (mayWrite) { - if (!mayWrite) { - self.debugLog("User doesn't have the right to edit."); - self.isBusy = false; - return; - } - try { - var ops = self.onOperation(socket, revision, operation, selection); - if (ops) { - self.debugLog("new operation: " + JSON.stringify(ops)); - self.socketOperations.push({ - socketId: socket.id, - revision: revision, - operation: JSON.parse(JSON.stringify(operation)), - selection: JSON.parse(JSON.stringify(selection)), - ops: JSON.parse(JSON.stringify(ops)) - }); - if (typeof self.operationCallback === 'function') { - self.operationCallback(socket, ops, function () { - self.isBusy = false; - }); - } else { + try { + var ops = self.onOperation(socket, revision, operation, selection); + if (ops) { + self.debugLog("new operation: " + JSON.stringify(ops)); + self.socketOperations.push({ + socketId: socket.id, + revision: revision, + operation: JSON.parse(JSON.stringify(operation)), + selection: JSON.parse(JSON.stringify(selection)), + ops: JSON.parse(JSON.stringify(ops)) + }); + if (typeof self.operationCallback === 'function') { + self.operationCallback(socket, ops, function () { self.isBusy = false; - } + }); } else { self.isBusy = false; } - } catch (err) { - self.logger.error(err); - setTimeout(function () { - socket.emit('doc', { - str: self.document, - revision: self.operations.length, - clients: self.users, - force: true - }); - }, 100); + } else { self.isBusy = false; } - }); - }) - .on('get_operations', function (base, head) { - self.onGetOperations(socket, base, head); - }) - .on('selection', function (obj) { - socket.origin = 'selection'; - self.mayWrite(socket, function (mayWrite) { - if (!mayWrite) { - self.debugLog("User doesn't have the right to edit."); - return; - } - self.updateSelection(socket, obj && Selection.fromJSON(obj)); - }); - }) - .on('disconnect', function () { - self.debugLog("Disconnect"); - socket.leave(self.docId); - self.onDisconnect(socket); + } catch (err) { + self.logger.error(err); + setTimeout(function () { + socket.emit('doc', { + str: self.document, + revision: self.operations.length, + clients: self.users, + force: true + }); + }, 100); + self.isBusy = false; + } + }); + }); + socket.on('get_operations', function (base, head) { + self.onGetOperations(socket, base, head); + }); + socket.on('selection', function (obj) { + socket.origin = 'selection'; + self.mayWrite(socket, function (mayWrite) { + if (!mayWrite) { + self.debugLog("User doesn't have the right to edit."); + return; + } + self.updateSelection(socket, obj && Selection.fromJSON(obj)); }); + }); + socket.on('disconnect', function () { + self.debugLog("Disconnect"); + socket.leave(self.docId); + self.onDisconnect(socket); + }); }; EditorSocketIOServer.prototype.onOperation = function (socket, revision, operation, selection) {