简体   繁体   English

RxJs 中的 mapTo 有什么意义?

[英]What's the point of mapTo in RxJs?

I'm reading the docs for mapTo and I'm simply not getting the point of that operator's existence.我正在阅读mapTo的文档,但我根本没有理解该运营商存在的意义。 I get what it does but the two line below should produce equivalent result.我明白了它的作用,但下面的两行应该产生等效的结果。

const outcome = "bzzz";
const result1 = source.map(a => outcome);
const result2 = source.mapTo(outcome);

Yet, the creators of RxJs chose to increase complexity by adding an extra method.然而,RxJs 的创建者选择通过添加额外的方法来增加复杂性。 It seems pointless as such, so I'm suspecting there's more to it than what meets the eye.这似乎毫无意义,所以我怀疑它的意义远不止眼前所见。

What's the point of having a dedicated operator for such a specific case?为这种特定情况配备专门的操作员有什么意义?

The differences between map and mapTo mapmapTo的区别

At first sight, we can see that map accepts a callback function乍一看,我们可以看到map接受一个回调 function

function map<T, R>(project: (value: T, index: number) => R, thisArg?: any)

whereas mapTo accepts a static valuemapTo接受static 值

function mapTo<R>(value: R)

So, a clear difference is that map 's callback function can generate a value depending on the arguments provided.因此,一个明显的区别是map的回调 function 可以根据提供的 arguments 生成一个值。 This is not the case for mapTo , where you just simply provide a value that will be always passed along to the next subscriber in the chain.这不是mapTo的情况,您只需提供一个值,该值将始终传递给链中的下一个订阅者。 If you will, mapTo(value) can be though of as如果你愿意, mapTo(value)可以是

map(() => value)

We can also deduce this from the source code:我们也可以从源代码中推断出:

// `mapTo`
() => subscriber.next(value)

// `map`; `project` is the provided callback function
() => subscriber.next(project.call(thisArg, value, index++));

But you might not want to do that, why call a function when you can directly use a value?但是您可能不想这样做,当您可以直接使用值时,为什么还要调用 function 呢?

Some use cases一些用例

A use case might be when you have a NgRx effect and you want to return the same, non-dynamic, action, when something happens.一个用例可能是当您具有 NgRx 效果并且您希望在发生某些事情时返回相同的、非动态的操作。

createEffect(
  () => this.actions.pipe(
    /* some logic here... */
    mapTo(staticAction('a message here')),
  )
);

Another one, which might not seem very practical, is when you want to simulate some requests coming in:另一种可能看起来不太实用的情况是,当您想要模拟一些请求时:

merge(
  timer(300).pipe(mapTo("req#1")),
  timer(600).pipe(mapTo("req#2")),
  timer(100).pipe(mapTo("req#3")),
  timer(900).pipe(mapTo("req#4")),
).pipe(
  /* some complicated logic here... */
)

Anyway, the point of this operator is, at least from my perspective, to indicate that you want to pass along static values, thus values that are not dependent on data from the previous subscriber.无论如何,至少从我的角度来看,这个运算符的意义在于表明您想要传递 static 值,因此这些值不依赖于前一个订阅者的数据。

mapTo is really just a convenience for a specific mapping scenario where: mapTo实际上只是为特定映射场景提供便利,其中:

  1. The output does not depend on emission from the source observable output 不依赖于可观察源的发射
  2. The output will always be the same static value output 将始终是相同的 static 值

The second point is important to note, because if the target value changes over time, there will be differences in the output between map(a => target) and mapTo(target) :第二点需要注意,因为如果目标值随时间变化, map(a => target)mapTo(target)之间的 output 会有差异:

  • map(a => outcome) - will return the current value of 'outcome' ( possibly different results ) map(a => outcome) results) - 将返回“结果”的当前值(可能不同的结果
  • mapTo(outcome) - will return the first emitted value of 'outcome' ( same result every time ) mapTo(outcome) - 将返回 'outcome' 的第一个发射值(每次都相同的结果
let outcome = 0;
// change 'outcome' every second
interval(1000).pipe(tap(n => outcome = n * 2)).subscribe();

const source = interval(1000).pipe(take(4));

source.pipe(map(a => outcome));  // 0, 2, 4, 6
source.pipe(mapTo(outcome));     // 0, 0, 0, 0

StackBlitz demo StackBlitz 演示

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

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