Rx js 理解提升方法 [英] Rx js understanding the lift method
问题描述
我想创建一个新的运算符,我在 文档,其中一种方法是做这样的事情:
I want to create a new operator and I find in the documentation that one of the ways is to do something like this:
class MyObservable extends Observable {
lift(operator) {
const observable = new MyObservable()
observable.source = this;
observable.operator = operator;
return observable;
}
// put it here .. or ..
customOperator() {
/* do things and return an Observable */
}
}
// ... put it here...
MyObservable.prototype.mySimpleOperator = mySimpleOperator;
我不明白什么是 lift
方法以及这里发生了什么,有人可以帮忙吗?
I don't understand what is the lift
method and what is going on here, can someone help, please?
推荐答案
lift
在 RxJS 5 内部一直使用.lift 的原理是你准备一个新的 Observable,在订阅时以操作员定义的方式转发事件.Paul Taylor 有一个关于它的好视频 (https://youtu.be/QhjALubBQPg?t=19m).Lift 是一个非常基础的构建块.
lift
is used all the time internally in RxJS 5. The principle of lift is that you prepare a new Observable that upon subscribe will forward the events in the way the operator defines. There is a good video about it by Paul Taylor (https://youtu.be/QhjALubBQPg?t=19m). Lift is a very fundamental building block.
不是创建一个新类——扩展 Observable——你也可以只创建 Operator 本身.操作员的用户然后可以通过编写来调用它:
Instead of creating a new class - extending Observable - you could also just create the Operator itself. Users of the operator can then call it by writing:
Observable.of(1, 2, 3)
.lift(new MyCustomOperator)
.subscribe()
这意味着没有人必须知道 Observable API 中还有另一个操作符可用,而是看到它是在别处定义的东西.
This means no-one has to learn that yet another operator is available in the Observable API, but instead sees that it is something defined elsewhere.
理想情况下你可以写
someObservable::myCustomOperator();
但不幸的是,绑定运算符可能会很远/永远不会发生,所以 .lift(operator)
似乎是最明确/最干净的方式.
but unfortunately the bind-operator might be long away / never gonna happen, so the .lift(operator)
seems like the most explicit / clean way.
这篇关于Rx js 理解提升方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!