switchMap to the completed observable doesn't complete the root observable #7036
Answered
by
josepot
artfulsage
asked this question in
Q&A
-
Hey! I see that interval(1000).pipe(switchMap(() => EMPTY)).subscribe(); |
Beta Was this translation helpful? Give feedback.
Answered by
josepot
Aug 10, 2022
Replies: 1 comment 6 replies
-
Yes, that is by design and it's the expected behavior. You could compose some of the existing operators to build a custom operator that behaves like that, though. For instance: import {
switchMap,
ObservableInput,
concat,
of,
pipe,
takeWhile,
OperatorFunction,
} from "rxjs"
// Notice that `DONE` is an implementation detail, meaning that the
// consumers of the custom operator wouldn't know about its existence,
const DONE = Symbol("DONE")
const emitDoneOnComplete = <T>(source: ObservableInput<T>) =>
concat(source, of(DONE))
export const switchMapUntilInnerCompletes = <I, O>(
project: (value: I) => ObservableInput<O>,
): OperatorFunction<I, O> =>
pipe(
switchMap(pipe(project, emitDoneOnComplete)),
takeWhile((value): value is O => value !== DONE),
) I haven't put much thought into its name though 😅 and I haven't tested it, but I'm quite certain that it should work 🙂 |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
artfulsage
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, that is by design and it's the expected behavior.
You could compose some of the existing operators to build a custom operator that behaves like that, though.
For instance: