简体   繁体   English

带有商店选择器的 concatMap 与 mergeMap

[英]concatMap vs mergeMap with store Selector

I have spent many time learning difference between mergeMap and concatMap to clearly understand the differnce, and it' is clear to me now.我花了很多时间学习mergeMapconcatMap之间的差异,以清楚地理解差异,现在我很清楚了。 I used good resources like https://rxjs-dev.firebaseapp.com and https://www.learnrxjs.io with other websites. I used good resources like https://rxjs-dev.firebaseapp.com and https://www.learnrxjs.io with other websites.

Both of this operators keeps the incoming source and subscribe to all emited values with difference in ordering.这两个运算符都保留传入的源并订阅所有发出的具有不同顺序的值。 But in my project i have met a situation when using mergeMap and concatMap differs a lot to me, and i want to understand why.但是在我的项目中,我遇到了一种情况,使用mergeMapconcatMap与我有很大不同,我想了解原因。

Here is a code sample:这是一个代码示例:

const Stream$ = from(requiredDocs.documents_type)
          .pipe(
            mergeMap(docID => this._store.select(selectDocumentTypeByID(docID))),
            scan((acc: Array<DocumentType | undefined>, curr) => {
              console.log('curr - ', curr)
              acc.push(curr)
              return acc
            }, [])
          ).subscribe(val => console.log(val))

Explanation : i am generating source from array requiredDocs.documents_type , then using values emitted to select from a store using selector - selectDocumentTypeByID(id: number) .说明:我正在从数组requiredDocs.documents_type生成源,然后使用选择器 - selectDocumentTypeByID(id: number)从商店发出到 select 的值。 The latter is a simple Array.find function that either returns undefined or a found object.后者是一个简单的 Array.find function,它返回未定义或找到的 object。 Then i use scan rxjs operator to generate resulting Array of values from selector然后我使用扫描rxjs 运算符从选择器生成结果数组

Expected result: [ {obj}, {obj}, {obj} ] - three objects that are returned by select observable预期结果: [ {obj}, {obj}, {obj} ] - select observable 返回的三个对象

mergeMap - result is as expected - [ {obj}, {obj}, {obj} ]

concatMap - result is just one obj(*first one*) - [ {obj} ]

If i change store selector to simple of(docID) rxjs operator it works as expected.如果我将存储选择器更改为简单的 of(docID) rxjs 运算符,它将按预期工作。

Why concatMap only returns one obj when using with store selector?为什么 concatMap 在与 store 选择器一起使用时只返回一个 obj?

Yes, magic of question asking works except that for this time with lots of pain and thinking:) Answer is in concatMap definition.是的,提问的魔法很有效,除了这次有很多痛苦和思考:) 答案在 concatMap 定义中。 This operator subscribes to each outer source Observable and only subscribes to following when previous has been completed该操作符订阅每个外部源 Observable 并且仅在上一个已完成时订阅后续

Funny, how question came up in my head suddenly and kinda randomly:) After many hours of thinking about it.有趣的是,我的脑海中突然出现了一个问题,有点随机:)经过几个小时的思考。 So, in order for my example to work properly i need to complete store.selector.因此,为了让我的示例正常工作,我需要完成 store.selector。 Which i yet don't know how to do, but to me the difference between these two operatos is now clear which is a major thing.我还不知道该怎么做,但对我来说,这两个歌剧之间的区别现在很清楚,这是一件重要的事情。

mergeMap in its turn does not wait for source Observable to finish and this is why it works as i needed.反过来,mergeMap 不会等待源 Observable 完成,这就是它按我需要工作的原因。 Cheers all:)祝大家好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM