RxJS运算符-订阅不是从嵌套运算符开始

时间:2018-12-03 13:45:04

标签: javascript typescript rxjs rxjs6 rxjs-pipeable-operators

我有以下代码段,该代码段无法按预期运行。 FooService有一个名为doFoo的函数,该函数返回从未正确订阅的Observable。在代码本身中,我留下了两行,它们指示代码在什么时候没有到达。

PS:过滤器使用的功能遍历非常庞大的列表,其中可能包含数百万个项目。

fooService.doFoo(layerId)
    .subscribe((tuple) => {
        console.log('Subscribed');  // Never reaches here
    })


FooService {


doFoo(layerId: string) {
    return this.getShoppingItems(layerId)
        .pipe (
            switchMap(shoppingItems => this.getMatchedShoppingItemsFromLayer((layerId, shoppingItems)))
        );
}

getMatchedShoppingItemsFromLayer(layerId, shoppingItems: ShoppingItems): Observable<ShoppingItemLayerTuple> {
    // Here the shoppingItems are arrived with no problem
    return this.shopLayerService.getLayerList(layerId)
        .pipe(
            switchMap((shopPartitions: ShopPartition) => shopPartitions.getPartitionList()),
            filter(partition => this.layerMatches(partition.getId(), shoppingItems)),
            map((partition) => {
                console.log('foo') // Never reaches here,
                return {bla, bla} as ShoppingItemLayerTuple;
            })
        );
}

layerMatches(partitionId: string, shoppingItems: ShoppingItems): boolean {
    shoppingItems.getItemsList().forEach((shoppingItem) => {
        const shoppingItemLayerId = '${fooPrefix}' + shoppingItem.getBla().getIdList()[0];
        if(partitionId === shoppingItemLayerId) {
            console.log('foo'); // Reaches here 27 times as expected
            return true;
        }
    });

    return false;   }

getShoppingItems(layerId: string): Observable<ShoppingItems> {
    return this.shopService.getSoppingItems(layerId);
}

}

0 个答案:

没有答案