NG Kit

Progress Util

A signal-based helper for tracking the lifecycle of asynchronous operations. initializeState returns a WritableSignal<ProgressState>, and markLoading, markSuccess, and markError drive it through loading, success, and error transitions so your UI updates reactively.

progress-util manages progress states in Angular applications using Angular Signals. It offers a simple, type-safe way to track the loading, success, and error states of asynchronous work, making it a good fit for form submissions, API calls, data loading, and user feedback.

The utility uses a WritableSignal for reactive state management, so the UI updates automatically when the progress state changes. Import the functions from @js-smart/ng-kit, initialize a state with initializeState(), and call the helpers to move through the lifecycle:

  • markLoading(state) — an operation is in progress.
  • markSuccess(state, message?) — the operation completed successfully.
  • markError(state, message?) — the operation failed.

The state follows a predictable flow: Initial → Loading → Success/Error, and can return to loading for a new operation. It works seamlessly with Angular change detection and the OnPush strategy.

API

Functions

FunctionParametersReturnsDescription
initializeStateWritableSignal<ProgressState>Initializes and returns a new progress state signal with default values
markLoadingstate: WritableSignal<ProgressState>voidUpdates the state to loading, clearing success and error flags
markSuccessstate: WritableSignal<ProgressState>, message?: stringvoidUpdates the state to success with an optional success message
markErrorstate: WritableSignal<ProgressState>, message?: stringvoidUpdates the state to error with an optional error message

ProgressState

PropertyTypeDescription
isLoadingbooleantrue when an operation is in progress, false otherwise
isSuccessbooleantrue when the last operation completed successfully
isErrorbooleantrue when the last operation failed
isCompleteboolean (optional)true when the operation has completed (success or error)
messagestringUser-friendly message describing the current state or outcome

The initial state is { isLoading: false, isSuccess: false, isError: false, isComplete: false, message: '' }. Reset for a new operation by calling markLoading() again.