rxjs-loading-state
Version:
Eliminates manual state management for loading and error states by transforming Observables into a LoadingState
89 lines (88 loc) • 2.53 kB
TypeScript
import { Observable } from "rxjs";
import { LoadingState } from "./loading-state";
/**
* Handles transitions between different loading state and holds the context data that is related to the current state.
* @class LoadingStateMachine
*/
export declare class LoadingStateMachine<T> {
private _state$;
private _data;
private _error;
constructor();
/**
* Creates a new observable that represents the current state of the machine
*
* @returns {Observable<LoadingState>} Observable that emits the machine state
*/
asObservable(): Observable<LoadingState>;
/**
* Data of the current state. Depending on the current state, this may be undefined.
*/
get data(): T | undefined;
/**
* Error of the current state. Depending on the current state, this may be undefined.
*/
get error(): any;
/**
* The current LoadingState
*/
get state(): LoadingState;
/**
* Update data while in loading state
* @param {T} newData
*/
update(newData?: T): void;
/**
* Starts loading
*/
start(): void;
/**
* Transition to success state
* @param {T} data
*/
succeed(data?: T): void;
/**
* Transition to error state
* @param {any} error
*/
fail(error: any): void;
/**
* Resets machine to not started
*/
reset(): void;
/**
* @returns {Boolean} True if machine if loading has not been started or reset
*/
isNotStarted(): boolean;
/**
* @returns {Boolean} True if machine is in loading state
*/
isLoading(): boolean;
/**
*
* @returns {Boolean} True if machine is in error state
*/
isError(): boolean;
/**
* @returns {Boolean} True if machine is in success state
*/
isSuccess(): boolean;
/**
* Factory to create a new machine in error state
* @param {any} error
* @returns {LoadingStateMachine<T>} The new LoadingStateMachine
*/
static asError<T>(error: any): LoadingStateMachine<T>;
/**
* Factory to create a new machine in success state
* @param {T} data
* @returns {LoadingStateMachine<T>} The new LoadingStateMachine
*/
static asSuccess<T>(data: T): LoadingStateMachine<T>;
/**
* Factory to create a new machine in loading state
* @param {T | undefined} data
* @returns {LoadingStateMachine<T>} The new LoadingStateMachine
*/
static asLoading<T>(data?: T | undefined): LoadingStateMachine<T>;
}