import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable, catchError, map, of, shareReplay } from 'rxjs'; import { WidgetManifestEntry, WidgetManifestFile } from '../contracts/widget-manifest.contract'; @Injectable({ providedIn: 'root' }) export class WidgetManifestService { private readonly manifestUrl = '/assets/mock/bootstrap/widget-manifest.json'; private manifest$?: Observable; constructor(private readonly http: HttpClient) {} getManifest(): Observable { if (!this.manifest$) { this.manifest$ = this.http.get(this.manifestUrl).pipe( shareReplay({ bufferSize: 1, refCount: true }), catchError(() => { this.manifest$ = undefined; return of({ widgets: [] }); }) ); } return this.manifest$; } getWidgets(): Observable { return this.getManifest().pipe(map((manifest) => manifest.widgets ?? [])); } getWidget(type: string): Observable { return this.getWidgets().pipe(map((widgets) => widgets.find((widget) => widget.type === type))); } }