UI widget used to indicate progress to user.
Creates the Promise we want the loading screen to wait on.
Will be called again if the original promise fails and the user clicks the try again button.
OptionalconvertReasonToMessage: (reason: unknown) => stringIf promise fails, this method will be passed the promise's error reason. Whatever string it returns will be used as the error message.
If convertReasonToMessage is left empty and the promise's reason is non-empty, the error message will default to new String(reason) instead. If reason is empty, the error message will be "Error ⚠".
In the following example, the user is attempting to import module "MainMenu". This will open the loading screen and activate the spinner animation. If the promise returned by importMainMenuModule is successful, it will simply turn off the loading screen.
If something goes terribly wrong, the loading screen will display an error message and give the user the option to try again, which will result in importMainMenuModule again being called.
//Import the modules we need.
import { LoadingScreen, awaitPromiseFunction } from "@crow281/light-loading-screen";
//Create the new loading screen.
const loadingScreen = new LoadingScreen();
//Function to load the main menu.
//It will be called once while attempting to load.
//If the returned promise triggers an error, it will offer the
//user the chance to call it again via the try again button.
const importMainMenuModule = (): Promise<any> => {
//Use dynamic import to load a desired module.
return import("./MainMenu");
}
//Function to generate the error message.
//Using this is optional.
//Default implementation will attempt to convert reason to a string.
//Dynamic imports are expected to pass
//a subclass of the Error type as the reason.
const convertReasonToMessage = (reason: Error): string => {
return reason.message
}
//Activate the loading screen and wait for importMainMenuModule to succeed.
//User can keep trying again until it succeeds,
//but cancelling has not been enabled.
awaitPromiseFunction(
loadingScreen,
importMainMenuModule,
convertReasonToMessage
);
Intended for operations that may fail but offer the user the chance to try again.
Sets the loading screen to animate until parameter promise completes.
If the promise is successful, the loading screen will then remove itself.
If the promise fails, it will display the error and a try again button. If the try again button is clicked, it will call createPromise again and wait on the new promise with the same rules as the original.