简体   繁体   English

从外部Rx.Observable.prototype访问可观察的源

[英]Accessing source observable from outside Rx.Observable.prototype

I am having trouble figuring out how to access the source observable, in this scheme (just trying to figure out how to this without modifying Rx.Observable.prototype): 在这种方案中,我很难弄清楚如何访问可观察的源(只是试图弄清楚如何在不修改Rx.Observable.prototype的情况下):

      q.drain()
        .flatMap(function(val){
            return q.backpressure(val, function(cb){
                   setTimeout(cb,1000);
            });
        })

We call backpressure as a method on the Queue prototype: 我们称反压为Queue原型上的一种方法:

Queue.prototype.backpressure = function(val, fn){

    const source = ? // I don't know how to access the source observable...

    return Rx.Observable.create(sub => {

        return source.subscribe(val => {

                fn.call(source, val, function(err, val){
                    if(err){
                        sub.error(err);
                    }
                    else{
                        sub.next(val);
                    }

                });
            },
            // be sure to handle errors and completions as appropriate and
            // send them along
            err => sub.error(err),
            () => sub.complete());

    });
};

but the problem is I don't know if I can access the source observable in this scheme - the correct value for source is certainly not the this value inside the prototype because that belongs to the queue instance. 但是问题是我不知道我是否可以访问此方案中可观察到的源-正确的源值肯定不是原型内的this值,因为它属于队列实例。 My only hope I think is somehow to pass the source observable directly into the backpressure method. 我认为,我唯一的希望就是以某种方式将可观察到的源直接传递给背压方法。 Anyone know how I can this? 有人知道我该怎么做吗? I don't mind putting this function elsewhere, it doesn't have to be a method on queue, but I think the same problem will exist either way. 我不介意将此函数放在其他地方,它不必一定要排队,但我认为无论哪种方式都将存在相同的问题。

If it helps, the value for this inside the flatMap function (if you use a regular function instead of an arrow function) is a MergeMapSubcriber object, see: 如果有帮助,该值this的flatMap函数内(如果使用常规的功能,而不是一个箭头功能)是一个MergeMapSubcriber对象,请参阅:

在此处输入图片说明

However, after experimenting, I don't believe that the MergeMapSubcriber value is the one I want to use as my source; 但是,经过试验后,我不认为MergeMapSubcriber值是我要用作源的值。 my source should be an Observable TMK, not a Subscriber. 我的来源应该是可观察的TMK,而不是订阅服务器。

Have you thought about putting it on Observable prototype? 您是否考虑过将其放在Observable原型上?

Observable.prototype.backpressure = function(queue, fn){
  const source = this;

  return this.flatMap(function(val){
    return Rx.Observable.create(sub => {

      return source.subscribe...
    });
  })
};

Then for queue: 然后对于队列:

q.drain()
  .backpressure(q, function(cb) {
    setTimeout(cb,1000);
  });

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

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