简体   繁体   中英

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.

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. Which means it will only ever show up on existing Observables as an instance method. You need to add it to the Observable as Observable.mySimpleOperator .

Internally you need to change source.interval(500) to Observable.interval(500) which is the static method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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