LoadingService

Manage loading indicators in your application with LoadingService.


LoadingService is a simple service designed to manage the state of loading indicators in your application. It provides methods to show or hide loading indicators and allows components to listen for changes in loading state.

Usage

import { LoadingService } from '@resourge/http-service';

// Show loading indicator
LoadingService.show();

// Show loading indicator with custom loaderId
LoadingService.show('myLoaderId');

// Hide loading indicator
LoadingService.hide();

// Hide loading indicator with custom loaderId
LoadingService.hide('myLoaderId');


// Add an event listener for the default loader
const removeListener = LoadingService.addListener(() => {
  // Handle loading state change
  console.log('Loading state changed!');
});

// Add an event listener for a specific loaderId
const removeCustomListener = LoadingService.addListener('myLoaderId', () => {
  // Handle loading state change for custom loaderId
  console.log('Custom Loading state changed!');
});

// To remove the listener later
removeListener(); // or removeCustomListener();

API

getLoading(loaderId?: string): boolean

  • Returns the current loading state for the specified loaderId. If no loaderId is provided, returns the default loading state.

addListener(loaderId?: string, onEmit: () => void): () => void

  • Adds an event listener for the specified loaderId. When the loading state changes, the onEmit function will be called.
  • Returns a function to remove the listener when no longer needed.

show(loaderId?: string): void

  • Shows the loading indicator for the specified loaderId. If no loaderId is provided, shows the default loading indicator.

hide(loaderId?: string): void

  • Hides the loading indicator for the specified loaderId. If no loaderId is provided, hides the default loading indicator.