import { Observable } from '../Observable'; import { ReplaySubject } from '../ReplaySubject'; import { IScheduler } from '../Scheduler'; import { Subscription } from '../Subscription'; import { MonoTypeOperatorFunction } from '../interfaces'; import { Subscriber } from '../Subscriber'; /** * @method shareReplay * @owner Observable */ export function shareReplay(bufferSize?: number, windowTime?: number, scheduler?: IScheduler ): MonoTypeOperatorFunction { return (source: Observable) => source.lift(shareReplayOperator(bufferSize, windowTime, scheduler)); } function shareReplayOperator(bufferSize?: number, windowTime?: number, scheduler?: IScheduler) { let subject: ReplaySubject; let refCount = 0; let subscription: Subscription; let hasError = false; let isComplete = false; return function shareReplayOperation(this: Subscriber, source: Observable) { refCount++; if (!subject || hasError) { hasError = false; subject = new ReplaySubject(bufferSize, windowTime, scheduler); subscription = source.subscribe({ next(value) { subject.next(value); }, error(err) { hasError = true; subject.error(err); }, complete() { isComplete = true; subject.complete(); }, }); } const innerSub = subject.subscribe(this); return () => { refCount--; innerSub.unsubscribe(); if (subscription && refCount === 0 && isComplete) { subscription.unsubscribe(); } }; }; };