UNPKG

@rxap/data-source

Version:

Provides a set of classes and decorators for creating and managing data sources in Angular applications, including base classes, static data sources, observable data sources, and method data sources. It also includes a component for displaying data source

114 lines (107 loc) 4.24 kB
import * as i0 from '@angular/core'; import { Injectable, InjectionToken, inject } from '@angular/core'; import { toSignal } from '@angular/core/rxjs-interop'; import { BaseDataSource } from '@rxap/data-source'; import { ToggleSubject } from '@rxap/rxjs'; import { isPromiseLike } from '@rxap/utilities'; import { ReplaySubject, Subject, isObservable } from 'rxjs'; import { tap } from 'rxjs/operators'; class AccordionDataSource extends BaseDataSource { constructor() { super(...arguments); this._data$ = new ReplaySubject(1); this.loading$ = new ToggleSubject(true); this.loading = toSignal(this.loading$, { initialValue: false }); this.hasError = toSignal(this.hasError$, { initialValue: false }); this.parameters = null; this._refresh$ = new Subject(); this._subscription = null; } get refresh$() { return this._refresh$.asObservable(); } ngOnDestroy() { super.ngOnDestroy(); this._subscription?.unsubscribe(); } ngOnInit() { const parameters = this.getParameters(); if (isObservable(parameters)) { this._subscription = parameters.subscribe((params) => { this.parameters = params; return this.load(); }); } else if (isPromiseLike(parameters)) { parameters.then((params) => { this.parameters = params; return this.load(); }); } else { this.parameters = parameters; this.load(); } } refresh() { const result = this.load(); if (isPromiseLike(result)) { result.then(() => this._refresh$.next()); } else { this._refresh$.next(); } } async load() { if (!this.parameters) { throw new Error('The parameters are not set. Ensure the parameters are set before calling the load method.'); } this.loading$.enable(); this.hasError$.disable(); try { this._data$.next(await this.method.call(this.parameters)); } catch (error) { console.error(`Fail to load data '${this.id}' - '${this.constructor.name}'`); this.hasError$.enable(); } finally { this.loading$.disable(); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: AccordionDataSource, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: AccordionDataSource }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: AccordionDataSource, decorators: [{ type: Injectable }] }); const ACCORDION_DATA_SOURCE = new InjectionToken('ACCORDION_DATA_SOURCE'); class PanelAccordionDataSource extends AccordionDataSource { constructor() { super(...arguments); this.accordionDataSource = inject(ACCORDION_DATA_SOURCE); } getParameters() { return this.accordionDataSource.getParameters(); } ngOnInit() { super.ngOnInit(); this._subscription = this.accordionDataSource.refresh$.pipe(tap(() => this.refresh())).subscribe(); } ngOnDestroy() { super.ngOnDestroy(); this._subscription?.unsubscribe(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: PanelAccordionDataSource, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: PanelAccordionDataSource }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.1", ngImport: i0, type: PanelAccordionDataSource, decorators: [{ type: Injectable }] }); // region // endregion /** * Generated bundle index. Do not edit. */ export { ACCORDION_DATA_SOURCE, AccordionDataSource, PanelAccordionDataSource }; //# sourceMappingURL=rxap-data-source-accordion.mjs.map