【问题标题】:How to chain RxJS5 operators into a new operator?如何将 RxJS5 运算符链接到新的运算符中?
【发布时间】:2016-03-04 11:01:21
【问题描述】:

根据operator creation guide,我尝试将一些我曾经使用过的运算符链接到另一个运算符,但没有任何成功。

function mySimpleOperator(actionName, iterable$, functionThatReturnAnObservable) {
   return Observable.create(subscriber => {
     var source = this;
     var subscription = source
       .interval(500)
       .skipUntil(iterable$.filter(({ action }) => action.type === actionName))
       .take(1)
       .flatMap(functionThatReturnAnObservable)
       .subscribe(value => {
         try {
           subscriber.next(value);
         } catch(err) {
           subscriber.error(err);
         }
     }, 
     err => subscriber.error(err),
     () => subscriber.complete());

     return subscription;
   });
}

Observable.prototype.mySimpleOperator = mySimpleOperator;

这个函数只是开始一个间隔,并且会被跳过,直到 actionName 被发出。

但是当我尝试使用我的运算符时

Observable.mySimpleOperator('APP_READY', source$, () => Observable.of({ type: 'DONE' })

会报错

Observable.mySimpleOperator is not a function

但是如果我在我的新运营商之外进行间隔调用,它会起作用吗?!

Observable.interval(500).mySimpleOperatorWithoutIntervall('APP_READY', source$, () => Observable.of({ type: 'DONE' })

有什么解决办法吗? :)

【问题讨论】:

    标签: javascript reactive-programming rxjs rxjs5


    【解决方案1】:

    您尚未将运算符添加到您将其添加到Observable.prototype 对象的对象中。这意味着它只会作为实例方法出现在现有的Observables 上。您需要将其添加到 Observable 为 Observable.mySimpleOperator

    在内部您需要将source.interval(500) 更改为Observable.interval(500),这是静态方法。

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 2021-08-16
      • 2014-04-12
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多