Skip to content

Commit 5e9e181

Browse files
committed
feat: stop chaining method calls on socket to compatible with socketio v3
1 parent 1b025c9 commit 5e9e181

File tree

1 file changed

+72
-73
lines changed

1 file changed

+72
-73
lines changed

lib/editor-socketio-server.js

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -54,91 +54,90 @@ EditorSocketIOServer.prototype.debugLog = function (msg) {
5454

5555
EditorSocketIOServer.prototype.addClient = function (socket) {
5656
var self = this;
57-
socket
58-
.join(this.docId)
59-
.emit('doc', {
60-
str: this.document,
61-
revision: this.operations.length,
62-
clients: this.users
63-
})
64-
.on('operation', function (revision, operation, selection, data) {
65-
self.isBusy = true;
66-
socket.origin = 'operation';
67-
if (data) {
68-
try {
69-
self.debugLog("new operation event: " + JSON.stringify(data));
70-
if (typeof self.operationEventCallback === 'function') {
71-
self.operationEventCallback(socket, data, function () {
72-
self.isBusy = false;
73-
});
74-
} else {
57+
socket.join(this.docId)
58+
socket.emit('doc', {
59+
str: this.document,
60+
revision: this.operations.length,
61+
clients: this.users
62+
})
63+
socket.on('operation', function (revision, operation, selection, data) {
64+
self.isBusy = true;
65+
socket.origin = 'operation';
66+
if (data) {
67+
try {
68+
self.debugLog("new operation event: " + JSON.stringify(data));
69+
if (typeof self.operationEventCallback === 'function') {
70+
self.operationEventCallback(socket, data, function () {
7571
self.isBusy = false;
76-
}
77-
} catch (err) {
78-
self.logger.error(err);
72+
});
73+
} else {
7974
self.isBusy = false;
8075
}
76+
} catch (err) {
77+
self.logger.error(err);
78+
self.isBusy = false;
79+
}
80+
return;
81+
}
82+
self.mayWrite(socket, function (mayWrite) {
83+
if (!mayWrite) {
84+
self.debugLog("User doesn't have the right to edit.");
85+
self.isBusy = false;
8186
return;
8287
}
83-
self.mayWrite(socket, function (mayWrite) {
84-
if (!mayWrite) {
85-
self.debugLog("User doesn't have the right to edit.");
86-
self.isBusy = false;
87-
return;
88-
}
89-
try {
90-
var ops = self.onOperation(socket, revision, operation, selection);
91-
if (ops) {
92-
self.debugLog("new operation: " + JSON.stringify(ops));
93-
self.socketOperations.push({
94-
socketId: socket.id,
95-
revision: revision,
96-
operation: JSON.parse(JSON.stringify(operation)),
97-
selection: JSON.parse(JSON.stringify(selection)),
98-
ops: JSON.parse(JSON.stringify(ops))
99-
});
100-
if (typeof self.operationCallback === 'function') {
101-
self.operationCallback(socket, ops, function () {
102-
self.isBusy = false;
103-
});
104-
} else {
88+
try {
89+
var ops = self.onOperation(socket, revision, operation, selection);
90+
if (ops) {
91+
self.debugLog("new operation: " + JSON.stringify(ops));
92+
self.socketOperations.push({
93+
socketId: socket.id,
94+
revision: revision,
95+
operation: JSON.parse(JSON.stringify(operation)),
96+
selection: JSON.parse(JSON.stringify(selection)),
97+
ops: JSON.parse(JSON.stringify(ops))
98+
});
99+
if (typeof self.operationCallback === 'function') {
100+
self.operationCallback(socket, ops, function () {
105101
self.isBusy = false;
106-
}
102+
});
107103
} else {
108104
self.isBusy = false;
109105
}
110-
} catch (err) {
111-
self.logger.error(err);
112-
setTimeout(function () {
113-
socket.emit('doc', {
114-
str: self.document,
115-
revision: self.operations.length,
116-
clients: self.users,
117-
force: true
118-
});
119-
}, 100);
106+
} else {
120107
self.isBusy = false;
121108
}
122-
});
123-
})
124-
.on('get_operations', function (base, head) {
125-
self.onGetOperations(socket, base, head);
126-
})
127-
.on('selection', function (obj) {
128-
socket.origin = 'selection';
129-
self.mayWrite(socket, function (mayWrite) {
130-
if (!mayWrite) {
131-
self.debugLog("User doesn't have the right to edit.");
132-
return;
133-
}
134-
self.updateSelection(socket, obj && Selection.fromJSON(obj));
135-
});
136-
})
137-
.on('disconnect', function () {
138-
self.debugLog("Disconnect");
139-
socket.leave(self.docId);
140-
self.onDisconnect(socket);
109+
} catch (err) {
110+
self.logger.error(err);
111+
setTimeout(function () {
112+
socket.emit('doc', {
113+
str: self.document,
114+
revision: self.operations.length,
115+
clients: self.users,
116+
force: true
117+
});
118+
}, 100);
119+
self.isBusy = false;
120+
}
121+
});
122+
})
123+
socket.on('get_operations', function (base, head) {
124+
self.onGetOperations(socket, base, head);
125+
})
126+
socket.on('selection', function (obj) {
127+
socket.origin = 'selection';
128+
self.mayWrite(socket, function (mayWrite) {
129+
if (!mayWrite) {
130+
self.debugLog("User doesn't have the right to edit.");
131+
return;
132+
}
133+
self.updateSelection(socket, obj && Selection.fromJSON(obj));
141134
});
135+
})
136+
socket.on('disconnect', function () {
137+
self.debugLog("Disconnect");
138+
socket.leave(self.docId);
139+
self.onDisconnect(socket);
140+
});
142141
};
143142

144143
EditorSocketIOServer.prototype.onOperation = function (socket, revision, operation, selection) {

0 commit comments

Comments
 (0)