All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.client.loader.Loader.ts.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
export abstract class Loader {
  static loading(): Loader {
    return new LoadingLoader();
  }

  static loaded(value: T): Loader {
    return new LoadedLoader(value);
  }

  static error(error: Error): Loader {
    return new ErrorLoader(error);
  }

  get isLoading(): boolean {
    return this.state === LoadingState.LOADING;
  }

  get isLoaded(): boolean {
    return this.state === LoadingState.LOADED;
  }

  get isInError(): boolean {
    return this.state === LoadingState.ERROR;
  }

  abstract get state(): LoadingState;

  abstract get value(): T;

  abstract get error(): Error;
}

export enum LoadingState {
  LOADING,
  LOADED,
  ERROR,
}

class LoadingLoader extends Loader {
  get state(): LoadingState {
    return LoadingState.LOADING;
  }

  get value(): T {
    throw new Error("Can't get value for a loading loaded");
  }

  get error(): Error {
    throw new Error("Can't get error for loading loader");
  }
}

class LoadedLoader extends Loader {
  constructor(private readonly _value: T) {
    super();
  }

  get state(): LoadingState {
    return LoadingState.LOADED;
  }

  get value(): T {
    return this._value;
  }

  get error(): Error {
    throw new Error("Can't get error for loaded loader");
  }
}

class ErrorLoader extends Loader {
  constructor(private readonly _error: Error) {
    super();
  }

  get state(): LoadingState {
    return LoadingState.ERROR;
  }

  get value(): T {
    throw new Error("Can't get value for error loader");
  }

  get error(): Error {
    return this._error;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy