fix: don't allow message sending before both sides are ready

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-07-05 21:23:15 +02:00
parent 57f7734f7f
commit 233fb263c7

View file

@ -42,6 +42,14 @@ export class MessageTransporter extends EventEmitter2<MessageEventPayloadMap> {
* @throws Error if no transport adapter has been set
*/
public sendMessage<M extends MessageType>(content: Message<M>): void {
if (this.transportAdapter === undefined) {
console.debug(
"Can't send message without transport adapter. Message that couldn't be sent was",
content
)
return
}
if (!this.isConnected()) {
this.onDisconnecting()
console.debug(
@ -51,6 +59,22 @@ export class MessageTransporter extends EventEmitter2<MessageEventPayloadMap> {
return
}
if (
!this.thisSideReady &&
content.type !== MessageType.READY_REQUEST &&
content.type !== MessageType.READY_ANSWER
) {
throw new Error("Can't send message. This side isn't ready")
}
if (
!this.otherSideReady &&
content.type !== MessageType.READY_REQUEST &&
content.type !== MessageType.READY_ANSWER
) {
throw new Error("Can't send message. Other side isn't ready")
}
try {
this.transportAdapter.send(content)
} catch (error: unknown) {