### File

`src/lib/method.data-source.ts`

### Extends

`BaseDataSource`

### Index

###### Properties
Protected Readonly _data$ Public Readonly loading$ Protected _connectedViewer Protected _connectedViewerTeardown Protected Optional _data Protected _lastRefreshed Protected _retry$ Protected _viewerIds Public Readonly change$ Public Readonly error$ Public Optional hasError Public Readonly hasError$ Public Optional loading
###### Methods
Protected _connect Protected Async execute Protected Async executeWithoutParameters Protected handelExecution Public refresh Protected _disconnect Public attach Public connect Public derive Public disconnect Protected genericRetryFunction Public getViewerId Protected handelError Public isConnected Public reset Public retry Public toJSON Public Async toPromise

### Constructor

`constructor(method: Method, withoutParameters)`
Defined in src/lib/method.data-source.ts:41
Parameters :
Name | Type | Optional
method | `Method<Data | Parameters>` | No
withoutParameters |  | No

### Methods

Protected _connect
`_connect(viewer: | BaseDataSourceViewer<Parameters>)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:65`
Parameters :
Name | Type | Optional
viewer | `| BaseDataSourceViewer<Parameters>` | No
Returns : `| Observable`

Protected Async execute
`execute(parameters?: Parameters)`
Defined in src/lib/method.data-source.ts:102
Parameters :
Name | Type | Optional
parameters | `Parameters` | Yes
Returns : `Promise<Data>`

Protected Async executeWithoutParameters
`executeWithoutParameters()`
Defined in src/lib/method.data-source.ts:106
Returns : `Promise<void>`

Protected handelExecution
`handelExecution()`
Defined in src/lib/method.data-source.ts:89
Returns : `OperatorFunction<Data, Data>`

Public refresh
`refresh()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:58`
Returns : `void`

Protected _disconnect
`_disconnect(viewerId: DataSourceViewerId)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:308`
Parameters :
Name | Type | Optional
viewerId | `DataSourceViewerId` | No
Returns : `void`

Public attach
`attach(viewerId: DataSourceViewerId)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:210`
Parameters :
Name | Type | Optional
viewerId | `DataSourceViewerId` | No
Returns : `Observable<Data>`

Public connect
`connect(viewerOrString: Viewer | DataSourceViewerId)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:124`
Parameters :
Name | Type | Optional
viewerOrString | `Viewer | DataSourceViewerId` | No
Returns : `Observable<Data>`

Public derive
`derive(id: string, metadata: Partial<BaseDataSourceMetadata>)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:270`
Parameters :
Name | Type | Optional | Default value
id | `string` | No |
metadata | `Partial<BaseDataSourceMetadata>` | No | `this.metadata`
Returns : `BaseDataSource<any>`

Public disconnect
`disconnect(viewerOrId: Viewer | DataSourceViewerId)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:225`
Parameters :
Name | Type | Optional
viewerOrId | `Viewer | DataSourceViewerId` | No
Returns : `void`

Protected genericRetryFunction
`genericRetryFunction(error: any, retryCount: number)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:311`
Parameters :
Name | Type | Optional
error | `any` | No
retryCount | `number` | No
Returns : `Observable<any>`

Public getViewerId
`getViewerId(viewer: Viewer)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:114`
Parameters :
Name | Type | Optional
viewer | `Viewer` | No
Returns : `string`

Protected handelError
`handelError(error: any)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:317`
Parameters :
Name | Type | Optional
error | `any` | No
Returns : `void`

Public isConnected
`isConnected(viewerOrId: Viewer | DataSourceViewerId)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:217`
Parameters :
Name | Type | Optional
viewerOrId | `Viewer | DataSourceViewerId` | No
Returns : `boolean`

Public reset
`reset()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:297`
Returns : `any`

Public retry
`retry()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:293`
Returns : `any`

Public toJSON
`toJSON()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:280`
Returns : `object`

Public Async toPromise
`toPromise(viewer: Viewer)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:264`
Creates a connection to tha data source and converts the Observable into a promise and then disconnects the viewer Parameters :
Name | Type | Optional
viewer | `Viewer` | No
Returns : `Promise<Data>`

### Properties

Protected Readonly _data$
Default value : `new ReplaySubject<Data>(1)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:37`

Public Readonly loading$
Default value : `new ToggleSubject(false)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:39`

Protected _connectedViewer
Default value : `new Map<DataSourceViewerId, Observable<Data>>()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:78`

Protected _connectedViewerTeardown
Default value : `new Map< DataSourceViewerId, TeardownLogic >()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:79`

Protected Optional _data
Type : `Data`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:104`

Protected _lastRefreshed
Type : `Date | null`
Default value : `null`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:77`

Protected _retry$
Default value : `new Subject<void>()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:90`

Protected _viewerIds
Default value : `new Map<Viewer, string>()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:89`
a map of viewer to view id. Allows to create a view id from the viewer object reference

Public Readonly change$
Default value : `new Subject<Data>()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:67`

Public Readonly error$
Default value : `new ReplaySubject<Error>(1)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:73`

Public Optional hasError
Type : `Signal<boolean>`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:94`

Public Readonly hasError$
Default value : `new ToggleSubject()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:72`

Public Optional loading
Type : `Signal<boolean>`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:92`

    import {
      Inject,
      Injectable,
      OnInit,
    } from '@angular/core';
    import { Method } from '@rxap/pattern';
    import { ToggleSubject } from '@rxap/rxjs';
    import {
      EMPTY,
      from,
      Observable,
      OperatorFunction,
      ReplaySubject,
      Subject,
      TeardownLogic,
    } from 'rxjs';
    import {
      startWith,
      switchMap,
      tap,
    } from 'rxjs/operators';
    import {
      BaseDataSource,
      BaseDataSourceMetadata,
      BaseDataSourceViewer,
    } from './base.data-source';
    import {
      RXAP_DATA_SOURCE_METHOD,
      RXAP_DATA_SOURCE_METHOD_WITHOUT_PARAMETERS,
    } from './tokens';

    @Injectable()
    export class MethodDataSource<Data, Parameters = any>
      extends BaseDataSource<Data, BaseDataSourceMetadata, (BaseDataSourceViewer & Parameters) | BaseDataSourceViewer<Parameters>>
      implements OnInit {

      protected override readonly _data$ = new ReplaySubject<Data>(1);

      public override readonly loading$ = new ToggleSubject(false);

      private readonly _refresh = new Subject<void>();

      constructor(
        @Inject(RXAP_DATA_SOURCE_METHOD)
        protected readonly method: Method<Data, Parameters>,
        @Inject(RXAP_DATA_SOURCE_METHOD_WITHOUT_PARAMETERS)
        protected readonly withoutParameters = false,
      ) {
        super(method.metadata);
      }

      public ngOnInit(): void {
        if (this.withoutParameters) {
          this.executeWithoutParameters();
        }
      }

      public override refresh(): void {
        this._refresh.next();
        if (this.withoutParameters) {
          this.executeWithoutParameters();
        }
      }

      protected override _connect(viewer: (BaseDataSourceViewer & Parameters) | BaseDataSourceViewer<Parameters>): [ Observable<Data>, TeardownLogic ] | Observable<Data> {
        if (this.withoutParameters) {
          return super._connect(viewer);
        } else {
          return this._refresh.pipe(
            tap(() => this.hasError$.disable()),
            startWith(undefined),
            switchMap<void, Observable<Data>>(() => {
              if (viewer.viewChange && viewer.viewChange !== EMPTY) {
                return (
                  viewer.viewChange as Observable<Parameters>
                ).pipe(
                  tap(() => this.loading$.enable()),
                  switchMap((parameters) => from(this.execute(parameters)).pipe(this.handelExecution())),
                );
              } else {
                this.loading$.enable();
                return from(this.execute(viewer as Parameters)).pipe(this.handelExecution());
              }
            }),
          );
        }
      }

      protected handelExecution(): OperatorFunction<Data, Data> {
        return tap({
          next: () => {
            this.loading$.disable();
          },
          error: (error: any) => {
            this.loading$.disable();
            this.hasError$.enable();
            this.error$.next(error);
          },
        });
      }

      protected async execute(parameters?: Parameters): Promise<Data> {
        return this.method.call(parameters);
      }

      protected async executeWithoutParameters(): Promise<void> {
        this.hasError$.disable();
        this.loading$.enable();
        try {
          const data = await this.method.call();
          this._data$.next(data);
        } catch (error: any) {
          this.hasError$.enable();
          this.error$.next(error);
        } finally {
          this.loading$.disable();
        }
      }

    }

# results matching ""

# No results matching ""