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

Reject promise returned from init if script loading fails #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ const handleOnLoad = (resolve: () => void, options: IInitObject) => {
});
}

const handleOnError = (resolve: () => void) => {
const handleOnError = (reject: () => void) => {
isOneSignalScriptFailed = true;
// Ensure that any unresolved functions are cleared from the queue,
// even in the event of a CDN load failure.
processQueuedOneSignalFunctions();
resolve();
reject();
}

const processQueuedOneSignalFunctions = () => {
Expand All @@ -56,7 +56,7 @@ const processQueuedOneSignalFunctions = () => {
});
}

const init = (options: IInitObject) => new Promise<void>(resolve => {
const init = (options: IInitObject) => new Promise<void>((resolve, reject) => {
if (isOneSignalInitialized) {
resolve();
return;
Expand All @@ -79,10 +79,10 @@ const init = (options: IInitObject) => new Promise<void>(resolve => {
handleOnLoad(resolve, options);
};

// Always resolve whether or not the script is successfully initialized.
// This is important for users who may block cdn.onesignal.com w/ adblock.
// Reject promise when the script is not successfully initialized.
// This is important for users who may block cdn.onesignal.com w/ adblock and want to understand if init was actually successful or not
script.onerror = () => {
handleOnError(resolve);
handleOnError(reject);
}

document.head.appendChild(script);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-onesignal",
"version": "2.0.3",
"version": "2.0.4",
"description": "React OneSignal Module: Make it easy to integrate OneSignal with your React App!",
"author": "rgomezp",
"contributors": [{ "name": "Rodrigo Gomez-Palacio" }, { "name": "Pedro Bini" }, { "name": "Graham Marlow" }],
Expand Down