Skip to content
Snippets Groups Projects
Select Git revision
  • dadf1ef4afcfd0f7f0e09f68ff6f4b88ad23209d
  • main default protected
  • release
  • OZG-7856_schadcode_scanner
  • ci-pipeline
  • OZG-7526-signatur-nicht-uebernommen
  • OZG-6223-zip-download-bug
  • OZG-7367-tooltip-extension
  • OZG-7023-OZG-6956-E2E-externe-Stellen
  • OZG-6238-npm-durch-pnpm-ersetzen
  • release-admin
  • release-info
  • OZG-6700-admin-feature-toggle
  • E2E-Updates
  • OZG-7047-tooltips
  • OZG-6957-e2e-fachstellen-oe-daten
  • OZG-7006-ZuarbeitAnfragen
  • temp_OZG-7027
  • unit-tests-hotfix
  • OZG-6731-POC-keycloakResourceService-with-multiple-stateResources
  • e2e-add-zufi-version
  • 2.26.0
  • 2.25.0
  • 2.24.2
  • 2.24.1
  • 2.24.0
  • 2.23.0
  • 2.22.0
  • 2.21.0
  • 2.20.0
  • 2.21.0-SNAPSHOT
  • 2.19.0
  • 2.18.0
  • 2.17.1
  • 1.3.0
  • release-admin-1.3.0
  • release-info-1.3.0
  • 2.17.0
  • 2.16.0
  • 2.15.0
  • release-admin-1.1.0
41 results

resource.service.ts

Blame
  • resource.service.ts 6.08 KiB
    import {
      BehaviorSubject,
      Observable,
      catchError,
      combineLatest,
      filter,
      map,
      mergeMap,
      of,
      startWith,
      tap,
      throwError,
    } from 'rxjs';
    import { ResourceServiceConfig } from './resource.model';
    import {
      StateResource,
      createEmptyStateResource,
      createErrorStateResource,
      createStateResource,
      isLoadingRequired,
      throwErrorOn,
    } from './resource.util';
    import { Resource, getUrl, hasLink } from '@ngxp/rest';
    import { ResourceRepository } from './resource.repository';
    import { isEqual, isNull } from 'lodash-es';
    import { isNotNull } from '../tech.util';
    import { HttpErrorResponse } from '@angular/common/http';
    import { isUnprocessableEntity } from '../http.util';
    import { HttpError, ProblemDetail } from '../tech.model';
    import { isDevMode } from '@angular/core';
    
    /**
     * B = Type of baseresource
     * T = Type of the resource which is working on
     */
    export class ResourceService<B extends Resource, T extends Resource> {
      readonly stateResource: BehaviorSubject<StateResource<T>> = new BehaviorSubject(
        createEmptyStateResource(),
      );
    
      configResource: B;
    
      constructor(
        private config: ResourceServiceConfig<B>,
        private repository: ResourceRepository,
      ) {}
    
      public get(): Observable<StateResource<T>> {
        return combineLatest([this.stateResource.asObservable(), this.getConfigResource()]).pipe(
          tap(([, configResource]) => this.handleConfigResourceChanged(configResource)),
          tap(([, configResource]) => this.handleNullConfigResource(configResource)),
          tap(([stateResource, configResource]) =>
            this.handleConfigResource(stateResource, configResource),
          ),
          filter(([stateResource]) => !this.shouldFilter(stateResource)),
          map(([stateResource]) => stateResource),
          startWith(createEmptyStateResource<T>(true)),
        );
      }
    
      private getConfigResource(): Observable<B> {
        return this.config.resource.pipe(
          filter((stateResource) => stateResource.loaded && !stateResource.loading),
          map((stateResource) => stateResource.resource),
        );
      }
    
      handleConfigResourceChanged(configResource: B): void {
        if (!isEqual(this.configResource, configResource)) {
          this.configResource = configResource;