Fastkit

vue-scoped-loading

Headless UI for generating a loading scope in a Vue application

Installation

npm install @fastkit/vue-scoped-loading

useLoading
function

# Signature
(): LoadingScope

Retrieve the loading scope of the application root

# Parameters
NameTypeDefault
# Return Type
LoadingScope

Loading scope of the application root

LoadingScope
class

Loading scope

root
readonly

LoadingScope

Loading scope of the application root

router
readonly
optional

undefined | Router

Vue Router instance

requests
readonly

LoadingRequest[]

List of loading requests

currentDisplaySettings
readonly

undefined | LoadingDisplaySettings

Current display settings

isIdle
readonly

boolean

Idle state

Indicates that there are no loading requests.

isPending
readonly

boolean

Pending state

Indicates that no loading is currently displayed but one or more requests are pending to be displayed.

isDisplaying
readonly

boolean

Displaying state

Indicates that one or more loading displays are currently active.

isActive
readonly

boolean

Active state

Indicates that one or more loading displays are either pending or active.

progress
readonly

number

Progress

0-100

Always 100 when there are no active loading requests.

create
function

# Signature
<Fn extends Callable>(fn: Fn, options?: LoadingRequestOptions): WithLoadingRequest<Fn>

Retrieve a function wrapped with loading display processing

Example

// 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 !!!
# Parameters
NameTypeDefault
fn*Fn
Function to be wrapped with loading display processing
optionsLoadingRequestOptions | undefined
Loading request options
# Return Type
WithLoadingRequest<Fn>

Function wrapped with loading display processing

createProgressHandler
function

# Signature
<Fn extends Callable>(handler: (request: LoadingRequest) => Fn, options?: LoadingRequestOptions): Fn

Generate a proxy function for loading display with progress processing

Example

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');
};
# Parameters
NameTypeDefault
handler*(request: LoadingRequest) => Fn
Handler returning asynchronous processing executed within the loading request scope
optionsLoadingRequestOptions | undefined
Loading request options
# Return Type
Fn

Function wrapped with loading display processing

request
function

# Signature
<Fn extends (request: LoadingRequest) => any>(fn: Fn, options?: LoadingRequestOptions): ReturnType<Fn>

Request the execution of a function with loading display

Example

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;
    }),
  ])
});
# Parameters
NameTypeDefault
fn*Fn
Function to be executed with loading display
optionsLoadingRequestOptions | undefined
Loading request options
# Return Type
ReturnType<Fn>

Result of the function execution

endAll
function

# Signature
(): void

End all loading displays

# Parameters
NameTypeDefault
# Return Type
void

End all loading displays

LoadingRequest
class

Loading request

state
readonly

"idle" | "pending" | "displaying"

State

isIdle
readonly

boolean

Idle state

isPending
readonly

boolean

Waiting to be displayed

isDisplaying
readonly

boolean

Currently displaying

isActive
readonly

boolean

Either waiting to be displayed or currently displaying

progress

number

Progress

0-100

Always 0 when not active

start
function

# Signature
(): void

Start loading display

This method usually does not need to be called manually by the application. Loading is automatically displayed when the function is executed.

# Parameters
NameTypeDefault
# Return Type
void

Start loading display

This method usually does not need to be called manually by the application. Loading is automatically displayed when the function is executed.

end
function

# Signature
(): void

End loading display

This method usually does not need to be called manually by the application. Loading is automatically hidden when the function execution ends.

# Parameters
NameTypeDefault
# Return Type
void

End loading display

This method usually does not need to be called manually by the application. Loading is automatically hidden when the function execution ends.

LoadingRequestOptions
class

Loading request options