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);
  }

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

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

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

  abstract state(): LoadingState;

  abstract value(): T;

  abstract error(): Error;
}

export enum LoadingState {
  LOADING,
  LOADED,
  ERROR,
}

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

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

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

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

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

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

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

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy