All files / src/services/util newAndStoredMulticast.ts

100% Statements 11/11
100% Branches 2/2
100% Functions 8/8
100% Lines 10/10

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 31 32 33  46x                     46x       3x 2x     288x 61x   9x 9x 9x       59x      
import { Logger } from 'ts-log';
import { Observable, from, groupBy, map, merge, mergeMap, share, tap } from 'rxjs';
 
interface NewAndStoredMulticast<T, K> {
  new$: Observable<T>;
  stored$: Observable<T[]>;
  storedFilterfn?: (value: T, index: number, array: T[]) => boolean;
  logger: Logger;
  logStringfn?: (stored: T[]) => string;
  groupByFn: (value: T) => K;
}
 
export const newAndStoredMulticast = <T, K>({
  new$,
  stored$,
  logger,
  storedFilterfn = () => true,
  logStringfn = () => '',
  groupByFn
}: NewAndStoredMulticast<T, K>) =>
  merge<Array<T>>(
    new$.pipe(map((evt) => evt)),
    stored$.pipe(
      tap((stored) => logger.debug(logStringfn(stored))),
      map((stored) => stored.filter(storedFilterfn)),
      mergeMap((stored) => from(stored))
    )
  ).pipe(
    groupBy(groupByFn),
    map((group$) => group$.pipe(share())),
    share()
  );