最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rx js understanding the lift method - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked May 4, 2017 at 5:16 ng2userng2user 2,0776 gold badges25 silver badges31 bronze badges 1
  • 1 The original GitHub proposal explains it well: github.com/ReactiveX/RxJS/issues/60 – cartant Commented May 4, 2017 at 6:00
Add a comment  | 

1 Answer 1

Reset to default 20

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.

发布评论

评论列表(0)

  1. 暂无评论