I'm working on an Angular 7 / Typescript / RxJS 6.3.3 project, using various RxJS Observables and related operators to process hierarchical collections (tree-like, specifically) of objects retrieved from a database via http server.
I thought to use the expand
operator to create a depth-first search, and used concatMap
to keep order... or so I hoped. Doesn't work.
See distilled example here: https://stackblitz.com/edit/rxjs-vf4zem
The console output from that example is:
dfs no delay: [1,8,9,22,23,24,2,4,7,3]
dfs with delay: [1,2,3,4,7,8,9,22,23,24]
(The second output line may vary depending on how much delay is added. The delay is intended to simulate data fetch from the http server.)
Given the data in the example, my desire is to consistently get the first line of output: a depth-first ordering. The key function from the example:
const dfs = (getter: Getter) => rootIds.pipe(
concatMap(ids => from(ids)),
expand(id =>
getter(id).pipe(
concatMap(children => from(children))
)
),
toArray()
);
Is there a way to enforce depth-first processing? Can expand
not guarantee that, or is this just a poor means to accomplish getting hierarchical data into a flattened, depth-first array?