From 682b001bdd6cb1c7d845e07ac69bf262201a5160 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 13 Jul 2023 14:40:14 +0100 Subject: [PATCH] Add a warning if contentLoaded event isn't received (#85) * Add a warning if contentLoaded event isn't received So the widget fails less silently if waitForIFrameLoad=false is specified but the widget doesn't send the contentLoaded event, to help prevent people like me being daft and wasting ages because they reverted one bit of code and not the other. * Revert dev package.json changes --- src/ClientWidgetApi.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/ClientWidgetApi.ts b/src/ClientWidgetApi.ts index ad67c74..fd8b049 100644 --- a/src/ClientWidgetApi.ts +++ b/src/ClientWidgetApi.ts @@ -120,6 +120,7 @@ export class ClientWidgetApi extends EventEmitter { private allowedEvents: WidgetEventCapability[] = []; private isStopped = false; private turnServers: AsyncGenerator | null = null; + private contentLoadedWaitTimer?: ReturnType; /** * Creates a new client widget API. This will instantiate the transport @@ -238,20 +239,30 @@ export class ClientWidgetApi extends EventEmitter { } else { // Reaching this means, that the Iframe got reloaded/loaded and // the clientApi is awaiting the FIRST ContentLoaded action. + console.log("waitForIframeLoad is false: waiting for widget to send contentLoaded"); + this.contentLoadedWaitTimer = setTimeout(() => { + console.error( + "Widget specified waitForIframeLoad=false but timed out waiting for contentLoaded event!", + ); + }, 10000); this.contentLoadedActionSent = false; } } private handleContentLoadedAction(action: IContentLoadedActionRequest) { + if (this.contentLoadedWaitTimer !== undefined) { + clearTimeout(this.contentLoadedWaitTimer); + this.contentLoadedWaitTimer = undefined; + } if (this.contentLoadedActionSent) { - throw new Error("Improper sequence: ContentLoaded Action can only be send once after the widget loaded " + throw new Error("Improper sequence: ContentLoaded Action can only be sent once after the widget loaded " +"and should only be used if waitForIframeLoad is false (default=true)"); } if (this.widget.waitForIframeLoad) { this.transport.reply(action, { error: { message: "Improper sequence: not expecting ContentLoaded event if " - +"waitForIframLoad is true (default=true)", + +"waitForIframeLoad is true (default=true)", }, }); } else {