簡體   English   中英

如何將RxJS5運算符鏈接到新運算符?

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

根據操作員創建指南 ,我試圖將一些我以前使用過的操作員鏈接到另一個操作員,但是沒有成功。

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' })

有什么解決辦法嗎? :)

您尚未將運算符添加到將其添加到Observable.prototype對象的對象中。 這意味着它將僅作為實例方法顯示在現有Observables 您需要將其作為Observable.mySimpleOperator添加到Observable.mySimpleOperator

在內部,您需要將source.interval(500)更改為Observable.interval(500) ,這是靜態方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM