Variant of takeUntil() which completes the source Observable when the notifier Observable completes #7352
kalgon
started this conversation in
Ideas / Feature request
Replies: 2 comments
-
You could do: source$.pipe(takeUntil(
notifier$.pipe(endWith(null))
)) |
Beta Was this translation helpful? Give feedback.
0 replies
-
FYI: it's not a party foul to write your own operators. But if you don't want to do that, and you don't want to test them, then go with @josepot's suggestion. You could do this: import { OperatorFunction, ObservableInput, from } from 'rxjs';
export function takeUntilComplete<T>(notifier: ObservableInput<unknown>): OperatorFunction<T, T> {
return (source) => new Observable(subscriber => {
subscriber.add(
from(notifier).subscribe({
complete: () => subscriber.complete(),
error: (err) => subscriber.error(err)
})
)
source.subscribe(subscriber)
})
} Which in RxJS 8 (alpha) still works 100%, and always will, but if you did it with the new import { OperatorFunction, ObservableInput, from, operate, noop } from 'rxjs';
export function takeUntilComplete<T>(notifier: ObservableInput<unknown>): OperatorFunction<T, T> {
return (source) => new Observable(destination => {
from(notifier).subscribe(operate({
destination,
next: noop,
}))
source.subscribe(subscriber)
})
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Would a variation of
takeUntil()
which would complete the sourceObservable
when the notifierObservable
completes make sense?In the following case, we wouldn't need to call
next()
on the notifier anymore:This could be a new operator (
takeUntilCompletes(notifier$)
) or the existing one could be augmented (takeUntil<T>(notifier: ObservableInput<any>, on: 'emits' | 'completes' = 'emits'): MonoTypeOperatorFunction<T>
)Beta Was this translation helpful? Give feedback.
All reactions