### File

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

### Extends

`BaseDataSource`

### Index

###### Properties

- Public Readonly dataSource
- Protected _connectedViewer
- Protected _connectedViewerTeardown
- Protected Optional _data
- Protected _data$
- Protected _lastRefreshed
- Protected _retry$
- Protected _viewerIds
- Public Readonly change$
- Public Readonly error$
- Public Optional hasError
- Public Readonly hasError$
- Public Optional loading
- Public loading$

###### Methods

- Protected _connect
- Public refresh
- Public reset
- Public setOperations
- Public toJSON
- Protected viewerToOperations
- Protected _disconnect
- Public attach
- Public connect
- Public derive
- Public disconnect
- Protected genericRetryFunction
- Public getViewerId
- Protected handelError
- Public isConnected
- Public retry
- Public Async toPromise

###### Accessors

- operations

### Constructor

`constructor(dataSource: BaseDataSource<Source>, operation: OperatorFunction | null, operations: Array<OperatorFunction | PipeDataSourceMetadata>)`
Defined in src/lib/pipe.data-source.ts:54

Parameters :

Name | Type | Optional
dataSource | `BaseDataSource<Source>` | No
operation | `OperatorFunction<Source | any> | null` | No
operations | `Array<OperatorFunction | PipeDataSourceMetadata>` | No

### Methods

Protected _connect
`_connect(viewer: BaseDataSourceViewer)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:124`

Parameters :

Name | Type | Optional
viewer | `BaseDataSourceViewer` | No

Returns : `| Observable`

Public refresh
`refresh(parent: boolean)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:105`

Parameters :

Name | Type | Optional | Default value | Description
parent | `boolean` | No | `this.metadata['refreshParent'] ?? false` | true - call the refresh method of the parent data source

Returns : `any`

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

Public setOperations
`setOperations(operation: OperatorFunction, ...operations: Array<OperatorFunction<any, any>>)`
Defined in src/lib/pipe.data-source.ts:83

Parameters :

Name | Type | Optional
operation | `OperatorFunction<Source | any>` | No
operations | `Array<OperatorFunction<any, any>>` | No

Returns : `void`

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

Protected viewerToOperations
`viewerToOperations(viewer: BaseDataSourceViewer)`
Defined in src/lib/pipe.data-source.ts:140

Parameters :

Name | Type | Optional
viewer | `BaseDataSourceViewer` | No

Returns : `Array<OperatorFunction<any, any>>`

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 retry
`retry()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:293`
Returns : `any`

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

Public Readonly dataSource
Type : `BaseDataSource<Source>`
Decorators : `@Inject(RXAP_DATA_SOURCE)`
Defined in src/lib/pipe.data-source.ts:57

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 _data$
Type : `Observable<Data>`
Default value : `EMPTY`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:83`

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`

Public loading$
Type : `Observable<boolean>`
Default value : `EMPTY`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:71`
Indicates weather the data source is currently loading new data

### Accessors

operations
get `operations()`
Defined in src/lib/pipe.data-source.ts:79

    import {
      Inject,
      Injectable,
      OnDestroy,
      Optional,
    } from '@angular/core';
    import { Constructor } from '@rxap/utilities';
    import {
      Observable,
      OperatorFunction,
      Subject,
      TeardownLogic,
    } from 'rxjs';
    import {
      map,
      startWith,
      switchMap,
    } from 'rxjs/operators';
    import {
      BaseDataSource,
      BaseDataSourceMetadata,
      BaseDataSourceViewer,
      RxapDataSource,
    } from './base.data-source';
    import { RxapDataSourceError } from './error';
    import {
      RXAP_DATA_SOURCE,
      RXAP_PIPE_DATA_SOURCE_OPERATOR,
    } from './tokens';

    export interface PipeDataSourceMetadata extends BaseDataSourceMetadata {
      /**
       * If true the parent data source will be refreshed when the pipe data source is refreshed
       * @default false
       */
      refreshParent?: boolean;
    }

    export function RxapPipeDataSource(
      metadata: PipeDataSourceMetadata,
      className = 'PipeDataSource',
      packageName = '@rxap/data-source',
    ) {
      return function (target: Constructor<PipeDataSource>) {
        RxapDataSource(metadata, className, packageName)(target);
      };
    }

    @Injectable()
    export class PipeDataSource<Source = any, Target = Source>
      extends BaseDataSource<Target>
      implements OnDestroy {

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

      constructor(
        @Inject(RXAP_DATA_SOURCE) public readonly dataSource: BaseDataSource<Source>,
        @Optional() @Inject(RXAP_PIPE_DATA_SOURCE_OPERATOR) operation: OperatorFunction<Source, any> | null = null,
        ...operations: Array<OperatorFunction<any, any> | PipeDataSourceMetadata>
      ) {
        super(dataSource.metadata);
        this.loading$ = this.dataSource.loading$;
        if (operation) {
          this.setOperations(
            operation,
            ...operations.filter(op => typeof op === 'function') as Array<OperatorFunction<any, any>>,
          );
          if (operations.some(op => typeof op !== 'function')) {
            this.metadata = {
              ...this.metadata,
              ...operations.find(op => typeof op !== 'function'),
            };
          }
        }
      }

      private _operations: Array<OperatorFunction<any, any>> = [];

      protected get operations(): Array<OperatorFunction<any, any>> {
        return this._operations;
      }

      public setOperations(
        operation: OperatorFunction<Source, any>,
        ...operations: Array<OperatorFunction<any, any>>
      ): void {
        if (this._initialised) {
          throw new RxapDataSourceError(
            'Can not set operations after the data source is initialised',
            '',
            'PipeDataSource',
          );
        }
        this._operations = [ operation, ...operations ];
      }

      public override ngOnDestroy() {
        super.ngOnDestroy();
        this.dataSource.disconnect(this.metadata);
      }

      /**
       * @param parent true - call the refresh method of the parent data source
       */
      public override refresh(parent: boolean = this.metadata['refreshParent'] ?? false): any {
        if (parent) {
          this.dataSource.refresh();
        } else {
          this._refresh.next();
        }
      }

      public override reset(): any {
        return this.dataSource.reset();
      }

      public override toJSON(): object {
        return {
          ...super.toJSON(),
          dataSource: this.dataSource,
        };
      }

      protected override _connect(viewer: BaseDataSourceViewer): [ Observable<Target>, TeardownLogic ] | Observable<Target> {
        this.init();
        return [
          this.dataSource.connect(viewer).pipe(
            switchMap(data => this._refresh.pipe(
              startWith(null),
              map(() => data),
            )),
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            ...this.viewerToOperations(viewer),
          ),
          () => this.dataSource.disconnect(viewer),
        ];
      }

      protected viewerToOperations(viewer: BaseDataSourceViewer): Array<OperatorFunction<any, any>> {
        return this.operations;
      }

    }

    export function pipeDataSource<Source, Target>(
      dataSource: BaseDataSource<Source>,
      operation: OperatorFunction<Source, any>,
      ...operations: Array<OperatorFunction<any, any> | PipeDataSourceMetadata>
    ): PipeDataSource<Source, Target> {
      return new PipeDataSource<Source, Target>(dataSource, operation, ...operations);
    }

# results matching ""

# No results matching ""