Headless UI for generating a loading scope in a Vue application
npm install @fastkit/vue-scoped-loading(): LoadingScopeRetrieve the loading scope of the application root
| Name | Type | Default |
|---|
LoadingScopeLoading scope of the application root
Loading scope
LoadingScopeLoading scope of the application root
undefined | RouterVue Router instance
LoadingRequest[]List of loading requests
undefined | LoadingDisplaySettingsCurrent display settings
booleanIdle state
Indicates that there are no loading requests.
booleanPending state
Indicates that no loading is currently displayed but one or more requests are pending to be displayed.
booleanDisplaying state
Indicates that one or more loading displays are currently active.
booleanActive state
Indicates that one or more loading displays are either pending or active.
numberProgress
0-100
Always 100 when there are no active loading requests.
<Fn extends Callable>(fn: Fn, options?: LoadingRequestOptions): WithLoadingRequest<Fn>Retrieve a function wrapped with loading display processing
// 1. Assume there is an asynchronous function defined as follows
const someAsyncFunc = async (delayAmount: number) => {
await delay(delayAmount);
return {
message: 'Hello world !!!',
};
};
// 2. Obtain the root loading scope
// * In this example, we are obtaining the root scope, but you can also use useScopedLoading to use the nearest scope.
const loading = useLoading();
// 3. Retrieve the function wrapped with LoadingScope.create function
const withLoadingFunc = loading.create(someAsyncFunc);
async function someHandler() {
// 4. It is executable with exactly the same arguments and return value structure as the original function. Loading is displayed until this process is completed.
// Even if an execution-time exception occurs in someAsyncFunc(), the loading display will end.
const result = await withLoadingFunc(3000);
console.log(result.message); // > Hello world !!!| Name | Type | Default |
|---|---|---|
| fn* | Fn | |
Function to be wrapped with loading display processing | ||
| options | LoadingRequestOptions | undefined | |
Loading request options | ||
WithLoadingRequest<Fn>Function wrapped with loading display processing
<Fn extends Callable>(handler: (request: LoadingRequest) => Fn, options?: LoadingRequestOptions): FnGenerate a proxy function for loading display with progress processing
const loading = useLoading();
// The `proxyFn` function is a Proxy function defined by the `createProgressHandler` method.
// When called, it is possible to reference the `LoadingRequest` instance, and by setting progress within it, it will be reflected in the overall progress of the loading scope.
// If there are multiple loading display requests within the loading scope, their total value is calculated as the progress rate.
const proxyFn = loading.createProgressHandler(
(request) => async (delaySeconds: number) => {
if (delaySeconds === 0) return;
for (let current = 0; current < delaySeconds; current++) {
request.progress = (current / delaySeconds) * 100;
await delay(1000);
}
},
);
const someHandler = () => {
await proxyFn(3);
console.log('3 seconds have passed');
};| Name | Type | Default |
|---|---|---|
| handler* | (request: LoadingRequest) => Fn | |
Handler returning asynchronous processing executed within the loading request scope | ||
| options | LoadingRequestOptions | undefined | |
Loading request options | ||
FnFunction wrapped with loading display processing
<Fn extends (request: LoadingRequest) => any>(fn: Fn, options?: LoadingRequestOptions): ReturnType<Fn>Request the execution of a function with loading display
const loading = useLoading();
// The loading display will be shown until the function passed as the first argument completes, and the return value will be obtained upon completion.
// If an exception occurs during execution, the loading display will safely be hidden.
const result = await loadingScope.request(() => fetch('https://hoge.fuga.com'));
// The function can also reference the `LoadingRequest` instance via its first argument. You can set the progress rate and other settings.
const result = await loadingScope.request((request) => {
let succeeded = 0;
const increment = () => {
succeeded++;
request.progress = (succeeded / 3) * 100;
};
return Promise.all([
fetch('https://hoge.1.com').then((res) => {
increment();
return res;
}),
fetch('https://hoge.2.com').then((res) => {
increment();
return res;
}),
fetch('https://hoge.3.com').then((res) => {
increment();
return res;
}),
])
});| Name | Type | Default |
|---|---|---|
| fn* | Fn | |
Function to be executed with loading display | ||
| options | LoadingRequestOptions | undefined | |
Loading request options | ||
ReturnType<Fn>Result of the function execution
(): voidEnd all loading displays
| Name | Type | Default |
|---|
voidEnd all loading displays
Loading request
"idle" | "pending" | "displaying"State
booleanIdle state
booleanWaiting to be displayed
booleanCurrently displaying
booleanEither waiting to be displayed or currently displaying
numberProgress
0-100
Always 0 when not active
(): voidStart loading display
This method usually does not need to be called manually by the application. Loading is automatically displayed when the function is executed.
| Name | Type | Default |
|---|
voidStart loading display
This method usually does not need to be called manually by the application. Loading is automatically displayed when the function is executed.
(): voidEnd loading display
This method usually does not need to be called manually by the application. Loading is automatically hidden when the function execution ends.
| Name | Type | Default |
|---|
voidEnd loading display
This method usually does not need to be called manually by the application. Loading is automatically hidden when the function execution ends.
Loading request options