import { CompositeDisposable, IDisposableOrSubscription } from '../../util'; import { ConnectableObservable, MonoTypeOperatorFunction, Observable } from 'rxjs'; import { IChangeSet } from '../IChangeSet'; import { publish } from 'rxjs/operators'; import { transform } from './transform'; import { disposeMany } from './disposeMany'; import { IPagedChangeSet } from '../IPagedChangeSet'; import { ISortedChangeSet } from '../ISortedChangeSet'; import { DistinctChangeSet } from '../DistinctChangeSet'; import { MonoTypeChangeSetOperatorFunction } from '../ChangeSetOperatorFunction'; /** * Subscribes to each item when it is added to the stream and unsubcribes when it is removed. All items will be unsubscribed when the stream is disposed * @typeparam TObject The type of the object. * @typeparam TKey The type of the key. * @param subscriptionFactory The subsription function */ export function subscribeMany(subscriptionFactory: (value: TObject, key: TKey) => IDisposableOrSubscription): MonoTypeChangeSetOperatorFunction { return function subscribeManyOperator(source: Observable>) { return new Observable>(observer => { const published = publish>()(source); const subscriptions = published.pipe(transform(subscriptionFactory), disposeMany()).subscribe(); return new CompositeDisposable(subscriptions, published.subscribe(observer), published.connect()); }); }; }