Rx js understanding the lift method

↘锁芯ラ 提交于 2020-01-23 04:46:35

问题


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;

I don't understand what is the lift method and what is going on here, can someone help, please?


回答1:


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.

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()

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.

Ideally you could write

someObservable::myCustomOperator();

but unfortunately the bind-operator might be long away / never gonna happen, so the .lift(operator) seems like the most explicit / clean way.



来源:https://stackoverflow.com/questions/43774407/rx-js-understanding-the-lift-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!