Currently, I am engaged in a project utilizing Angular 7, Typescript, and RxJS 6.3.3. In this project, I am working with RxJS Observables and relevant operators to handle hierarchical collections of objects obtained from an http server that interfaces with a database.
In order to create a depth-first search algorithm, I initially attempted to utilize the expand
operator while employing concatMap
to maintain order. Unfortunately, this approach proved ineffective.
You can find a simplified example here: https://stackblitz.com/edit/rxjs-vf4zem
The console output generated by this example is as follows:
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 sequence of the second output line may vary depending on the extent of the simulated data retrieval delay meant to mimic fetching data from the http server.)
With reference to the dataset provided in the example, my intention is to consistently obtain the first line of output: a representation following a depth-first order. The crucial function featured in the example is:
const dfs = (getter: Getter) => rootIds.pipe(
concatMap(ids => from(ids)),
expand(id =>
getter(id).pipe(
concatMap(children => from(children))
)
),
toArray()
);
Is there an effective method to enforce depth-first processing? Can the use of expand
guarantee such an outcome, or is it simply an inadequate strategy for transforming hierarchical data into a flattened, depth-first array?