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

finalize: pass the last emitted value to the callback #4803

Closed
thorn0 opened this issue May 20, 2019 · 2 comments
Closed

finalize: pass the last emitted value to the callback #4803

thorn0 opened this issue May 20, 2019 · 2 comments

Comments

@thorn0
Copy link
Contributor

thorn0 commented May 20, 2019

Feature Request

Is your feature request related to a problem? Please describe.
When my observable completes, errors, or gets unsubscribed, I need to do a cleanup using the last emitted value.

Describe the solution you'd like
I'd like to get the last emitted value as an argument to finally's callback

Describe alternatives you've considered
All the alternatives look too clumsy.

Additional context
Another issue about adding a callback argument to finalize: #2823

@cartant
Copy link
Collaborator

cartant commented May 22, 2019

IMO, this is something that should be handled in a user-land operator or observable. I don't think it's something that should be added to finalize.

@benlesh
Copy link
Member

benlesh commented May 28, 2019

You can compose this pretty easily with existing operators:

https://stackblitz.com/edit/rxjs-fkavym

function finalizeWithValue<T>(callback: (value: T) => void) {
  return (source: Observable<T>) => defer(() => {
    let lastValue: T;
    return source.pipe(
      tap(value => lastValue = value),
      finalize(() => callback(lastValue)),
    )
  })
}

I agree with @cartant that this should probably be a user land thing.

@benlesh benlesh closed this as completed May 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants