Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(shareReplay): align logic with var names #2919

Merged
merged 1 commit into from
Oct 9, 2017

Conversation

cartant
Copy link
Collaborator

@cartant cartant commented Oct 7, 2017

Description:

This PR changes isComplete from being always true to representing whether or not
the source is actually complete and changes the logical test accordingly.

Without the change, isComplete is always true and the unsubscribe call made using the subscription to the source can never occur - regardless of the refCount.

With the change, the call will happen, but only if the refCount is zero and the source has completed.

If the source has completed, the unsubscribe call is redundant and the refCount is irrelevant. If you want, the implementation could be simplified to take that into account, like this:

export function shareReplay<T>(bufferSize?: number, windowTime?: number, scheduler?: IScheduler ): MonoTypeOperatorFunction<T> {
  let subject: ReplaySubject<T>;
  let hasError = false;

  return (source: Observable<T>) => new Observable<T>(observer => {
    if (!subject || hasError) {
      hasError = false;
      subject = new ReplaySubject<T>(bufferSize, windowTime, scheduler);
      source.subscribe({
        next(value) { subject.next(value); },
        error(err) {
          hasError = true;
          subject.error(err);
        },
        complete() {
          subject.complete();
        },
      });
    }

    const innerSub = subject.subscribe(observer);

    return () => {
      innerSub.unsubscribe();
    };
  });
};

Related issue (if exists): None

Change isComplete from being always true to representing whether or not
the source is actually complete.
@rxjs-bot
Copy link

rxjs-bot commented Oct 7, 2017

Messages
📖

CJS: 1345.6KB, global: 745.3KB (gzipped: 139.9KB), min: 145.4KB (gzipped: 30.9KB)

Generated by 🚫 dangerJS

@coveralls
Copy link

coveralls commented Oct 7, 2017

Coverage Status

Coverage increased (+0.01%) to 97.467% when pulling 0d01628 on cartant:share-replay-logic into 92547a0 on ReactiveX:master.

@benlesh benlesh merged commit 4dc73e4 into ReactiveX:master Oct 9, 2017
@cartant cartant deleted the share-replay-logic branch March 31, 2018 02:24
@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants