promise-loading-spinner
Version:
Advanced handling of loaders/spinners based on one or multiple Promises
137 lines (136 loc) • 4.06 kB
TypeScript
export interface LoaderConfig {
/**
* Signal
*/
loaderVisibilityCallback: ((value: boolean) => void) | undefined;
/**
* Time (ms) until spinner will show up to handle short operations without a spinner.
*/
delay: number;
/**
* Time (ms) to wait until last promise is resolved to enable multiple operations in a sequence
* without a "flickering" spinner.
*/
closeDelay: number;
/**
* Delay (ms) after loader initialization to suppress spinners on page load.
*/
initDelay: number;
/**
* The element which represents the spinner.
* Can be used with an element or a selector string.
*/
loaderElement?: HTMLElement | string;
/**
* Class name used to show the spinner.
*/
classActive: string;
}
/**
* Call options for the public API functions that add a promise.
*/
export interface LoaderCallOptions {
/**
* Skip all delays (initDelay, delay).
*/
skipDelays: boolean;
}
/**
* Advanced handling of loaders/spinners based on one or multiple Promises.
*/
export default class Loader {
/**
* Contains all promises that aren't settled.
*/
private loaderPromises;
/**
* The configuration of the loader.
*/
private config;
/**
* The attached DOM element which represents the loader.
*/
private el?;
/**
* The timeout handle for the initial suppression of the loader.
*/
private initSuppressTimeout;
/**
* The timeout handle for the delay before showing the loader.
*/
private timeout;
/**
* The timeout handle for the closing delay.
*/
private closingTimeout;
/**
* Is the loader currently shown?
*/
protected loaderShows: boolean;
/**
* Resolver function used to resolve the currentLoadingPromise promise after the loader get hidden.
*/
private loaderShownResolver?;
/**
* The promises for which the loader currently shows up.
*/
private promisesForShownLoader;
/**
* A promise that resolves after the loader get hidden.
*/
currentLoadingPromise: Promise<Promise<unknown>[]>;
/**
* Constructor.
*
* @param cfg configuration of the loader (optional)
*/
constructor(cfg?: Partial<LoaderConfig>);
/**
* Add a promise to the loader.
*
* @param promise a promise
* @param options options for this promise (optional)
* @returns the used promise
*/
loader<T>(promise: Promise<T>, options?: Partial<LoaderCallOptions>): Promise<T>;
/**
* Returns a function that wraps the loader functionality around a function call.
*
* @param fnc A function performing some async operation
* @param options options for the operation (optional)
* @returns a function that wraps the loader functionality around a function call
*/
wrapFunction<C, A extends never[], R>(fnc: (this: C, ...arguments_: A) => Promise<R>, options?: Partial<LoaderCallOptions>): (this: C, ...arguments_: A) => Promise<R>;
/**
* A decorator for methods that wraps loader functionality around a function call.
* @param options options for the operation (optional)
* @returns a decorator for methods that wraps loader functionality around a function call.
*/
decorator(options?: Partial<LoaderCallOptions>): MethodDecorator;
/**
* Stops initial loader suppresion
*/
private stopSuppressLoading;
/**
* Create the promise for the currently shown loader.
*/
private setCurrentLoadingPromise;
/**
* Show or hide the loader.
*
* @param visible is the loader visible?
*/
private setLoaderVisibility;
/**
* Wait for promise to fulfill or reject and check if the loader can hide.
*
* @param promise the promise to process
*/
private handlePromise;
/**
* Show the loader. Also adds the loader delay.
*
* @param skipDelays skip delays?
*/
private showLoader;
}