简体   繁体   English

用运算符扩展RxJS Observable类

[英]Extend RxJS Observable class with operators

How can Observable class be extended by applying built-in RxJS operators to it? 如何通过将内置的RxJS运算符应用于它来扩展Observable类?

I would like to do something like this: 我想做这样的事情:

class TruthyObservable extends Observable {
  constructor(subscriber) {
    super(subscriber);

    return this.filter(x => x);
  }
}

class TruthyMappedObservable extends TruthyObservable {
  constructor(subscriber) {
    super(subscriber);

    return this.map(x => `'${x}'`);
  }
}

Can this be done without constructor return? 是否可以在没有构造函数返回的情况下完成?

This pretty much depends on what you want to do but let's say you want to make a TruthyObservable that behaves very much like the default Observable.create(...) but passes only even numbers: 这在很大程度上取决于您要执行的操作,但是假设您要使TruthyObservable行为与默认的Observable.create(...)非常相似,但仅传递偶数:

import { Observable, Observer, Subscriber, Subject, Subscription } from 'rxjs';
import 'rxjs/add/operator/filter';

class TruthyObservable<T> extends Observable<T> {

    constructor(subscribe?: <R>(this: Observable<T>, subscriber: Subscriber<R>) => any) {
        if (subscribe) {
            let oldSubscribe = subscribe;
            subscribe = (obs: Subscriber<any>) => {
                obs = this.appendOperators(obs);
                return oldSubscribe.call(this, obs);
            };
        }

        super(subscribe);
    }

    private appendOperators(obs: Subscriber<any>) {
        let subject = new Subject();

        subject
            .filter((val: number) => val % 2 == 0)
            .subscribe(obs);

        return new Subscriber(subject);
    }

}

let o = new TruthyObservable<number>((obs: Observer<number>) => {
    obs.next(3);
    obs.next(6);
    obs.next(7);
    obs.next(8);
});

o.subscribe(val => console.log(val));

This prints to console: 打印到控制台:

6
8

See live demo: https://jsbin.com/recuto/3/edit?js,console 观看现场演示: https : //jsbin.com/recuto/3/edit?js,控制台

Usually classes inheriting Observable override the _subscribe() method that actually makes the subscription internally but in ours case we want to use the callback where we can emit values by ourselves (since this Observable doesn't emit anything itself). 通常,继承Observable类会覆盖_subscribe()方法,该方法实际上是在内部进行预订的,但是在我们的示例中,我们希望使用回调函数,在回调函数中我们可以自己发出值(因为此Observable本身不会发出任何东西)。 Method _subscribe() is overshadowed by _subscribe property if it exists so we wouldn't be able to append any operators to it if we just overrode this method. _subscribe()方法_subscribe()如果存在_subscribe()_subscribe属性所遮盖,因此,如果仅覆盖此方法,我们将无法向其附加任何运算符。 That's why I wrap _subscribe in the constructor with another function and then pass all values through a Subject chained with filter() in appendOperators() method. 这就是为什么我用另一个函数将_subscribe包装在构造函数中,然后将所有值传递appendOperators()方法中的filter()链接的Subject appendOperators() Note that I replaced the original Observer with the Subject at obs = this.appendOperators(obs) . 请注意,我在obs = this.appendOperators(obs)处用Subject替换了原始Observer。

At the end when I call eg. 最后,当我打电话给。 obs.next(3); I'm in fact pushing values to the Subject that filters them and passes them to the original Observer . 实际上,我是在将值推送到Subject来过滤它们并将它们传递给原始的Observer

I think you can get what you need with custom operator: 我认为您可以通过自定义运算符获得所需的内容:

    Observable.prototype.truthy = function truthy() {
        return this.filter(x => x);
    }

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

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