import { IObservableCache } from '../IObservableCache'; import { concat, MonoTypeOperatorFunction, Observable } from 'rxjs'; import { filter, map, take } from 'rxjs/operators'; import { ChangeSet } from '../ChangeSet'; import { notEmpty } from './notEmpty'; import { IChangeSet } from '../IChangeSet'; import { statusMonitor } from './statusMonitor'; import { MonoTypeChangeSetOperatorFunction } from '../ChangeSetOperatorFunction'; /** * Defer the subscription until the stream has been inflated with data * @typeparam TObject The type of the object. * @typeparam TKey The type of the key. */ export function deferUntilLoaded(source: IObservableCache): Observable>; /** * Defer the subscription until the stream has been inflated with data * @typeparam TObject The type of the object. * @typeparam TKey The type of the key. */ export function deferUntilLoaded(): MonoTypeChangeSetOperatorFunction; export function deferUntilLoaded(source?: IObservableCache) { if (source !== undefined) { return concat( source.countChanged.pipe( filter(count => count != 0), take(1), map(_ => new ChangeSet()), ), source.connect(), ).pipe(notEmpty()); } return function deferUntilLoadedOperator(source: Observable>) { return concat( source.pipe( statusMonitor(), filter(status => status == 'loaded'), take(1), map(_ => new ChangeSet()), ), source, ).pipe(notEmpty()); }; }