简体   繁体   English

如何将RxJS5运算符链接到新运算符?

[英]How to chain RxJS5 operators into a new operator?

According to the operator creation guide , i tried to chain some operators i used to an another operator but without any success. 根据操作员创建指南 ,我试图将一些我以前使用过的操作员链接到另一个操作员,但是没有成功。

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;

This function simply start an interval and will be skip until the actionName will be emitted. 此函数只是开始一个时间间隔,并且将跳过该操作,直到发出actionName为止。

But when i tried to use my operator 但是当我尝试使用运算符时

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

It throw an error 抛出错误

Observable.mySimpleOperator is not a function

But if i do the intervall call outside my new operator it works ?! 但是,如果我在新的运算符之外进行间隔调用,它将起作用吗?

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

Any solutions ? 有什么解决办法吗? :) :)

You haven't added the operator to the object you added it to the Observable.prototype object. 您尚未将运算符添加到将其添加到Observable.prototype对象的对象中。 Which means it will only ever show up on existing Observables as an instance method. 这意味着它将仅作为实例方法显示在现有Observables You need to add it to the Observable as Observable.mySimpleOperator . 您需要将其作为Observable.mySimpleOperator添加到Observable.mySimpleOperator

Internally you need to change source.interval(500) to Observable.interval(500) which is the static method. 在内部,您需要将source.interval(500)更改为Observable.interval(500) ,这是静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM