首页 > 解决方案 > 是否有修改 observable 的 RxJS 操作符?

问题描述

这个问题是为了学习目的,而不是为了解决一个特定的问题(如果需要,请把它移到适当的部分)。

我正在学习 RxJS 库中的管道运算符。在此站点 ( https://rxjs.dev/guide/operators ) 上,它区分了可管道操作符和创建者操作符。

它定义了可管道操作符,如下所示:

Pipeable Operator 是一个将 Observable 作为其输入并返回另一个 Observable 的函数。这是一个纯粹的操作:之前的 Observable 保持不变。

它定义了创建者操作符如下:

Creation Operators 是另一种运算符,可以作为独立函数调用以创建新的 Observable。例如: of(1, 2, 3) 创建一个 observable,它将一个接一个地发出 1、2 和 3。

但这让我想知道:是否有这样的运算符可以修改它作为输入获得的可观察对象并将其作为输出返回?我还没有遇到过这样的事情。有没有这样的运营商不存在的原因?这样的操作员会导致什么样的不良行为?

标签: rxjspiperxjs-observablesrxjs-pipeable-operators

解决方案


You can see pipable operation as a series of function execution, in most of the time there's no need for modifying the upstream function. What we interest in is transforming data and add custom operation as we proceed down the stream

fn(fn2(fn3(...)))

if in any case you want to modify upstream behavior, the upstream observable has to be designed to allow such case, for instance use a function factory to let user add an middleware

e.g

const interceptor=()=>{...}
const getUpstreanFn=(middleware)=>(param)=>{ middleware()......}
const upstreamFn=getUpstreamFn(middleware)

推荐阅读