Skip to content

Commit

Permalink
feat: stop chaining method calls on socket to compatible with socketi…
Browse files Browse the repository at this point in the history
…o v3
  • Loading branch information
galaxian85 committed May 3, 2023
1 parent 1b025c9 commit 5e9e181
Showing 1 changed file with 72 additions and 73 deletions.
145 changes: 72 additions & 73 deletions lib/editor-socketio-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5e9e181

Please sign in to comment.