Recursion in RxJS #6703
Unanswered
Sawtaytoes
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Functions all the way :) I've had sometimes where I have a circular dependency between observables - In that case I break it out with a Subject. But in general, I think a recursive function should be alright, I think I would do it like this: declare function readDirContents(
dir: string
): Array<{ filename: string; isDirectory: boolean }>;
function getFiles$(directory: string): Observable<string> {
return from(readDirContents(directory)).pipe(
mergeMap((file) =>
file.isDirectory ? getFiles$(file.filename) : of(file.filename)
)
);
} However, for this simple case you also have function getFiles$(directory): Observable<string> {
return from(readDirContents(directory)).pipe(
expand((file) =>
file.isDirectory ? from(readDirContents(file.filename)) : of(file)
),
filter(({ isDirectory }) => !isDirectory),
map(({ filename }) => filename)
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I can think of a couple ways of doing recursion in RxJS and don't think either is really that intuitive to new RxJS developers.
I'm curious which you've seen and the situations you think these should be used (subject vs function):
Beta Was this translation helpful? Give feedback.
All reactions