diff --git a/packages/lib/src/link/connectors.ts b/packages/lib/src/link/connectors.ts index 999800ba..71bfc964 100644 --- a/packages/lib/src/link/connectors.ts +++ b/packages/lib/src/link/connectors.ts @@ -34,6 +34,7 @@ export abstract class BaseConnector extends events.EventEmitter { } protected abstract send(message: libData.MessageRoutable): void; + abstract get valid(): boolean; forward(message: libData.MessageRoutable) { const seq = this._seq; @@ -346,6 +347,14 @@ export abstract class WebSocketBaseConnector extends BaseConnector { get hasSession() { return ["connected", "resuming"].includes(this._state); } + + /** + * True if the connector is valid and can accept new messages, + * only needs to be checked on fringe cases during setup and tear down. + */ + get valid() { + return this.hasSession; + } } /** @@ -723,4 +732,14 @@ export class VirtualConnector extends BaseConnector { send(message: libData.Message) { this.other.emit("message", message); } + + /** + * True if the connector is valid and can accept new messages, + * only needs to be checked on fringe cases during setup and tear down. + * + * For a virtual connector this is always true. + */ + get valid() { + return true; + } } diff --git a/packages/lib/src/logging_utils.ts b/packages/lib/src/logging_utils.ts index b3b34d35..49470bd6 100644 --- a/packages/lib/src/logging_utils.ts +++ b/packages/lib/src/logging_utils.ts @@ -207,7 +207,9 @@ export class LinkTransport extends Transport { } try { - this.link.send(new libData.LogMessageEvent(info)); + if (this.link.connector.valid) { + this.link.send(new libData.LogMessageEvent(info)); + } } catch (err) { // Ignore session lost errors. if (!(err instanceof libErrors.SessionLost)) {