### File

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

### Extends

`BaseDataSource`

### Index

###### Properties

- Protected _data$
- 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
- Public loading$

###### Methods

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

###### Accessors

- data

### Constructor

`constructor(data: any | null, metadata: StaticDataSourceMetadata | null)`
Defined in src/lib/static.data-source.ts:47

Parameters :

Name | Type | Optional
data | `any | null` | No
metadata | `StaticDataSourceMetadata | null` | No

### Methods

Protected _connect
`_connect()`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:105`
Returns : `Observable<Data>`

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

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 _data$
Default value : `new ReplaySubject<Data>(1)`
Inherited from `BaseDataSource`
Defined in `BaseDataSource:46`

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`

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

data
get `data()`
Defined in src/lib/static.data-source.ts:90
set `data(value: Data)`
Defined in src/lib/static.data-source.ts:94

Parameters :

Name | Type | Optional
value | `Data` | No

Returns : `void`

    import {
      clone,
      Constructor,
      isPromise,
      isPromiseLike,
    } from '@rxap/utilities';
    import {
      isObservable,
      Observable,
      ReplaySubject,
      Subscription,
    } from 'rxjs';
    import {
      Inject,
      Injectable,
      OnDestroy,
      Optional,
    } from '@angular/core';
    import {
      RXAP_DATA_SOURCE_METADATA,
      RXAP_STATIC_DATA_SOURCE_DATA,
    } from './tokens';
    import {
      BaseDataSource,
      BaseDataSourceMetadata,
      RxapDataSource,
    } from './base.data-source';
    import { RxapDataSourceError } from './error';
    import { tap } from 'rxjs/operators';

    export type StaticDataSourceData<Data = any> =
      | Data
      | Promise<Data>
      | Observable<Data>
      | (() => Data);

    export interface StaticDataSourceMetadata<Data = any>
      extends BaseDataSourceMetadata {
      data?: StaticDataSourceData<Data>;
    }

    @Injectable()
    export class StaticDataSource<Data = any>
      extends BaseDataSource<Data, StaticDataSourceMetadata<Data>>
      implements OnDestroy {
      protected override _data$ = new ReplaySubject<Data>(1);
      private _dataSubscription?: Subscription;

      constructor(
        @Optional()
        @Inject(RXAP_STATIC_DATA_SOURCE_DATA)
          data: any | null,
        @Optional()
        @Inject(RXAP_DATA_SOURCE_METADATA)
          metadata: StaticDataSourceMetadata | null,
      ) {
        super(metadata);
        if ((data === undefined || data === null) && this.metadata.data !== undefined) {
          data = this.metadata.data;
        }
        if (data === undefined) {
          throw new RxapDataSourceError(
            `Can not create static data source '${ this.id }' with undefined as data`,
            '',
          );
        }
        if (isObservable(data)) {
          this._dataSubscription = (
            data as Observable<any>
          ).pipe(
            tap({
              next: data => this._data$.next(data),
              error: error => this.error$.next(error),
            }),
          ).subscribe();
        } else if (isPromiseLike(data)) {
          if (isPromise(data)) {
            data.then(data => this._data$.next(data)).catch((error) => this.error$.next(error));
          } else {
            data.then(data => this._data$.next(data));
          }
        } else if (typeof data === 'function') {
          // TODO : fix typeof function cast
          this._data$.next((this._data = (data as any)()));
        } else {
          this._data$.next((this._data = data));
        }
      }

      public override get data(): Data {
        return clone<Data>(this._data!);
      }

      public override set data(value: Data) {
        const data = clone(value);
        this._data = data;
        this._data$.next(data);
      }

      public override ngOnDestroy(): void {
        super.ngOnDestroy();
        this._dataSubscription?.unsubscribe();
      }

      protected override _connect(): Observable<Data> {
        this.init();
        return this._data$.asObservable();
      }

    }

    export function RxapStaticDataSource<Data>(
      metadata: StaticDataSourceMetadata<Data>,
      className = 'StaticDataSource',
      packageName = '@rxap/data-source',
    ) {
      return function (target: Constructor<StaticDataSource<Data>>) {
        RxapDataSource(metadata, className, packageName)(target);
      };
    }

    export function staticDataSource<Data>(
      data: StaticDataSourceData<Data>,
      metadata: StaticDataSourceMetadata<Data> = { id: 'static' },
    ): StaticDataSource<Data> {
      return new StaticDataSource(data, metadata);
    }

# results matching ""

# No results matching ""