Skip to content

Commit b5f5d6d

Browse files
committed
[FIX] session: always return a Promise
If the implementation of `transportService` doesn't return a Promise when calling `sendMessage`, the session breaks. With this commit, we ensure we always get a Promise which allows to safely call `.then` or `.catch` closes #7721 Task: 0 Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
1 parent 665fdc0 commit b5f5d6d

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

src/collaborative/readonly_transport_filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ReadonlyTransportFilter implements TransportService<CollaborationMe
1414
message.type === "CLIENT_LEFT" ||
1515
message.type === "CLIENT_MOVED"
1616
) {
17-
this.transportService.sendMessage(message);
17+
await this.transportService.sendMessage(message);
1818
}
1919
// ignore all other messages
2020
}

src/collaborative/session.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class Session extends EventBus<CollaborativeEvent> {
195195
}
196196
delete this.clients[this.clientId];
197197
this.transportService.leave(this.clientId);
198-
this.transportService.sendMessage({
198+
this.sendToTransport({
199199
type: "CLIENT_LEFT",
200200
clientId: this.clientId,
201201
version: MESSAGE_VERSION,
@@ -210,7 +210,7 @@ export class Session extends EventBus<CollaborativeEvent> {
210210
return;
211211
}
212212
const snapshotId = this.uuidGenerator.uuidv4();
213-
await this.transportService.sendMessage({
213+
await this.sendToTransport({
214214
type: "SNAPSHOT",
215215
nextRevisionId: snapshotId,
216216
serverRevisionId: this.serverRevisionId,
@@ -267,17 +267,15 @@ export class Session extends EventBus<CollaborativeEvent> {
267267
const type = currentPosition ? "CLIENT_MOVED" : "CLIENT_JOINED";
268268
const client = this.getCurrentClient();
269269
this.clients[this.clientId] = { ...client, position };
270-
this.transportService
271-
.sendMessage({
272-
type,
273-
version: MESSAGE_VERSION,
274-
client: { ...client, position },
275-
})
276-
.then(() => {
277-
if (this.pendingMessages.length > 0 && !this.waitingAck) {
278-
this.sendPendingMessage();
279-
}
280-
});
270+
this.sendToTransport({
271+
type,
272+
version: MESSAGE_VERSION,
273+
client: { ...client, position },
274+
}).then(() => {
275+
if (this.pendingMessages.length > 0 && !this.waitingAck) {
276+
this.sendPendingMessage();
277+
}
278+
});
281279
}
282280

283281
/**
@@ -382,7 +380,7 @@ export class Session extends EventBus<CollaborativeEvent> {
382380
if (client) {
383381
const { position } = client;
384382
if (position) {
385-
this.transportService.sendMessage({
383+
this.sendToTransport({
386384
type: "CLIENT_MOVED",
387385
version: MESSAGE_VERSION,
388386
client: { ...client, position },
@@ -406,6 +404,11 @@ export class Session extends EventBus<CollaborativeEvent> {
406404
this.sendPendingMessage();
407405
}
408406

407+
private async sendToTransport(message: CollaborationMessage) {
408+
// wrap in an async function to ensure it returns a promise
409+
return this.transportService.sendMessage(message);
410+
}
411+
409412
/**
410413
* Send the next pending message
411414
*/
@@ -433,17 +436,15 @@ export class Session extends EventBus<CollaborativeEvent> {
433436
${JSON.stringify(message)}`);
434437
}
435438
this.waitingAck = true;
436-
this.transportService
437-
.sendMessage({
438-
...message,
439-
serverRevisionId: this.serverRevisionId,
440-
})
441-
.catch((e: Error) => {
442-
if (!(e instanceof ClientDisconnectedError)) {
443-
throw e.cause || e;
444-
}
445-
this.waitingAck = false;
446-
});
439+
this.sendToTransport({
440+
...message,
441+
serverRevisionId: this.serverRevisionId,
442+
}).catch((e: Error) => {
443+
if (!(e instanceof ClientDisconnectedError)) {
444+
throw e.cause || e;
445+
}
446+
this.waitingAck = false;
447+
});
447448
}
448449

449450
private acknowledge(message: CollaborationMessage) {

0 commit comments

Comments
 (0)