Recursive calls to Firestore

后端 未结 2 634
滥情空心
滥情空心 2021-01-20 09:14

I have a Firebase Firestore with \"Components\" as a root collection. Each document (a \"Component\") in the collection may have an array called \"children\", with each ite

2条回答
  •  [愿得一人]
    2021-01-20 09:48

    Recursion in RxJS is best tackled with the use of the expand operator. You provide it with a projection function that returns an Observable, that, on notification, calls the projection function again with the emitted value. It does this for as long as your inner Observable is not emitting EMPTY or complete.

    While it does that, every notification is also forwarded to the subscribers of expand, unlike with a traditional recursion where you'll only get the result at the very end.

    From the official docs on expand:

    Recursively projects each source value to an Observable which is merged in the output Observable.

    Let's look at your example. Without RxJS, if we had a synchronous datasource that gave us each child of a node (let's call it getChildById), the function could look like this:

    function createFamilyTree(node) {
        node.children = node.childrenIds.map(childId => 
            createFamilyTree(
                getChildById(childId)
            )
        );
        return node;
    }
    

    Now, we'll translate it to RxJS with the use of the expand operator:

    parentDataSource$.pipe(
        map(parent => ({node: parent})),
    
        expand(({node}) => // expand is called for every node recursively 
                              (i.e. the parent and each child, then their children and so on)
    
            !node ? EMPTY : // if there is no node, end the recursion
    
            from(node.childrenIds) // we'll convert the array of children 
                                      ids to an observable stream
    
                .pipe(
                    mergeMap(childId => getChildById(childId) // and get the child for 
                                                                  the given id
    
                        .pipe(
                            map(child => ({node: child, parent: node}))) // map it, so we have 
                                                                           the reference to the 
                                                                           parent later on
                    )
                )
        ),
    
        // we'll get a bunch of notifications, but only want the "root", 
           that's why we use reduce:
    
        reduce((acc, {node, parent}) =>
            !parent ?
                node : // if we have no parent, that's the root node and we return it
                parent.children.push(node) && acc // otherwise, we'll add children
        )
    );
    

    The used operators are explained in detail on the official RxJS documentation page: from, expand, reduce

    EDIT: A clean and tested version of the above code can be found here: https://stackblitz.com/edit/rxjs-vzxhqf?devtoolsheight=60

提交回复
热议问题