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

Add clearBuffer method to ReplaySubject #6632

Conversation

crossan007
Copy link

@crossan007 crossan007 commented Oct 6, 2021

Description:
Adds a clearBuffer() method on ReplaySubject. This allows consumers to clear the buffer of events which have already been delivered to the ReplaySubject.

This addresses an edge case where a developer may want to avoid disrupting existing subscriptions to a ReplaySubject and prevent new subscribers from receiving the whole history before a point in time (while maintaining the behavior which facilitates new subscribers to receive history)

This should be a preferred solution over the various proposed workarounds at:

An alternative workaround may be to extend ReplaySubject as ClearableReplaySubject and adjust the visibility modifier on the events[] from private to protected so that this method can be implemented on the child class

BREAKING CHANGE:

Related issue (if exists):

@benlesh benlesh added the AGENDA ITEM Flagged for discussion at core team meetings label Jan 11, 2022
@benlesh
Copy link
Member

benlesh commented Jan 11, 2022

I'll leave it to the core team, and we'll discuss it, but In general, I'm inclined to say that we're probably not going to implement this feature for a few reasons:

  1. It introduces another vector for mutability to ReplaySubject.
  2. It has no tests
  3. The use cases aren't clearly defined.

As a workaround, someone could take a more reactive approach, particularly with the new share features:

const shared = source$.pipe(
  // If some stream of resets notifies, error with a known error (like "RESET")
  mergeWith(reset$.pipe(tap(() => { throw 'RESET' })),
  // multicast through a ReplaySubject. `share` will reset this
  // ReplaySubject on error by default.
  share({
    connector: () => new ReplaySubject(),
  }),
  retry({
    // If the error is the known error of "RESET", then reconnect everyone
    // if it's any other error, just throw it.
    delay: (err) => {
       if (err === 'RESET') {
         return of(true);
       } else {
         throw error;
       }
    }
  })
)

@benlesh
Copy link
Member

benlesh commented Jan 12, 2022

Core Team: We don't want to move forward with this one.

Thank you for your effort, @crossan007

@benlesh benlesh closed this Jan 12, 2022
@crossan007
Copy link
Author

crossan007 commented Jan 12, 2022

Okay, thanks for the feedback.

I'm using this implementation currently to consume AngularFirestore observables in various locations in my app, in a way that I can clear the replay buffer when AngularFireauth context changes.

I.e. user logs out / user changes, I don't want the old buffer available.

@douglascayers
Copy link

douglascayers commented Sep 30, 2024

If this helps anyone help, I've put @benlesh's example into a ResettableReplaySubject class.

Source code with example available at https://stackblitz.com/edit/rxjs-x4n1rt?file=index.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AGENDA ITEM Flagged for discussion at core team meetings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants