Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for widget loader #63

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/functions/_userback_widget.default.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h5>token: <span class="tsd-signature-type">string</span></h5></li>
<h5><code class="tsd-tag ts-flagOptional">Optional</code> ubOptions: <a href="../interfaces/_userback_widget.UserbackOptions.html" class="tsd-signature-type" data-tsd-kind="Interface">UserbackOptions</a></h5></li></ul></div>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><a href="../interfaces/_userback_widget.UserbackWidget.html" class="tsd-signature-type" data-tsd-kind="Interface">UserbackWidget</a><span class="tsd-signature-symbol">&gt;</span></h4><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/userback/widget-js/blob/master/widget-js/widget.ts#L143">widget-js/widget.ts:143</a></li></ul></aside></li></ul></section></div>
<li>Defined in <a href="https://github.com/userback/widget-js/blob/master/widget-js/widget.ts#L147">widget-js/widget.ts:147</a></li></ul></aside></li></ul></section></div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
2 changes: 1 addition & 1 deletion docs/functions/_userback_widget.getUserback.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1>Function getUserback</h1></div>
</div>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">undefined</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/_userback_widget.UserbackWidget.html" class="tsd-signature-type" data-tsd-kind="Interface">UserbackWidget</a></h4><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/userback/widget-js/blob/master/widget-js/widget.ts#L209">widget-js/widget.ts:209</a></li></ul></aside></li></ul></section></div>
<li>Defined in <a href="https://github.com/userback/widget-js/blob/master/widget-js/widget.ts#L215">widget-js/widget.ts:215</a></li></ul></aside></li></ul></section></div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<div class="tsd-navigation settings">
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
Expand Down
12 changes: 9 additions & 3 deletions widget-js/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,25 @@ let USERBACK: UserbackWidget | undefined;

/* eslint-enable no-unused-vars */
// internal variable for storing a pending load of the widget to prevent loading the widget twice
// When undefined, the widget is not currently loading.
let UBLoadingPromise: Promise<UserbackWidget> | undefined;
/*
* UserbackWidgetLoader
*
* Provides a type-safe interface for initializing and retrieving the Userback object
* @param token - The Userback token to use for initialisation
* @param ubOptions - Optional configuration options for the Userback widget
* @returns A promise that resolves to the UserbackWidget object
*/
export default function UserbackWidgetLoader(token: string, ubOptions?: UserbackOptions): Promise<UserbackWidget> {
if (UBLoadingPromise) return UBLoadingPromise;

UBLoadingPromise = new Promise((resolve, reject) => {
// Validation
const error = (e: string | Event) => reject(new Error(e.toString()));
const error = (e: string | Event) => {
UBLoadingPromise = undefined;
return reject(typeof e === 'string' ? new Error(e) : e);
};
if (typeof USERBACK !== 'undefined') {
// eslint-disable-next-line no-console
console.debug('Userback widget loaded twice, canceling initialisation');
Expand Down Expand Up @@ -195,11 +202,10 @@ export default function UserbackWidgetLoader(token: string, ubOptions?: Userback
script.src = `https://static.${ubDomain}/widget/v1.js`;
script.async = true;
script.onload = onload;
script.onerror = error;
script.addEventListener('error', error);
document.body.appendChild(script);
return true;
});
UBLoadingPromise.catch(() => { UBLoadingPromise = undefined; });
return UBLoadingPromise;
}

Expand Down
Loading