Koalabz
Koalabz

Reputation: 13

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 ? :)

Upvotes: 0

Views: 100

Answers (1)

paulpdaniels
paulpdaniels

Reputation: 18663

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.

Upvotes: 1

Related Questions