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
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.
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.
Most helpful comment
You can compose this pretty easily with existing operators:
https://stackblitz.com/edit/rxjs-fkavym
I agree with @cartant that this should probably be a user land thing.