All files / src/services/util persistentTrackerSubjects.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  46x 46x   46x     255x 255x     255x 255x       255x     260x 260x       46x   674x      
import { CollectionStore, DocumentStore } from '../../persistence';
import { Observable, concat, defaultIfEmpty, of, switchMap, tap } from 'rxjs';
import { TrackerSubject } from '@cardano-sdk/util-rxjs';
 
export class PersistentCollectionTrackerSubject<T> extends TrackerSubject<T[]> {
  readonly store: CollectionStore<T>;
  constructor(source: (localObjects: T[]) => Observable<T[]>, store: CollectionStore<T>) {
    const stored$ = store.getAll().pipe(defaultIfEmpty([]));
    super(
      stored$.pipe(
        switchMap((stored) => {
          const source$ = source(stored);
          return stored.length > 0 ? concat(of(stored), source$) : source$;
        })
      )
    );
    this.store = store;
  }
  next(value: T[]): void {
    this.store && this.store.setAll(value);
    super.next(value);
  }
}
 
export class PersistentDocumentTrackerSubject<T> extends TrackerSubject<T> {
  constructor(source$: Observable<T>, store: DocumentStore<T>) {
    super(concat(store.get(), source$.pipe(tap(store.set.bind(store)))));
  }
}