简体   繁体   English

RxJS首先取得油门然后等待

[英]RxJS take first then throttle and wait

I want to observer the mousewheel event using RxJS-DOM so that when the first event fires, I forward that on and then drop any and all values until the delay between subsequent values passes a previously specified duration. 我想使用RxJS-DOM观察mousewheel轮事件,以便在第一个事件触发时,我将其转发然后删除任何和所有值,直到后续值之间的延迟超过先前指定的持续时间。

The operator I'm imagining might look something like: 我想象的运算符可能看起来像:

Rx.DOM.fromEvent(window, 'mousewheel', (e) => e.deltaY)
.timegate(500 /* ms */)

Imagine the following data stream: 想象一下以下数据流:

0 - (200 ms) - 1 - (400ms) - 2 - (600ms) - 3

where the value being sent is the number and the time describes how long the next value takes to arrive. 其中发送的值是数字,时间描述下一个值到达的时间。 Since 0 is the first value, it would be emitted and then all values until 3 would be dropped because the individual delays between subsequent values are not greater than 500ms . 由于0是第一个值,它将被发射,然后直到3的所有值都将被丢弃,因为后续值之间的各个延迟不大于500ms

Unlike throttle, the time delay between values is calculated whether or not the last received value is emitted or not. 与节流阀不同,计算值之间的时间延迟,无论是否发出最后接收的值。 With throttle, 0 would be send, 200 ms would elapse, 1 would be evaluated and fail, 400 ms would elapse and 2 would be evaluated and PASS because the time elapse between the last emitted value (0) and the currently received one (2) is 600 ms whereas with my operator, it would evaluate relative to the 1 and the time elapse would be 400 ms, therefore failing the test. 使用油门,0将被发送,200 ms将被过去,1将被评估并失败,400 ms将被过去,2将被评估和PASS,因为最后一个发射值(0)和当前接收的值之间经过的时间(2 )是600毫秒,而对于我的操作员,它将相对于1进行评估,并且经过的时间将是400毫秒,因此测试失败。

And this operator isn't debounce either. 而且这个运营商也没有去抖动。 Instead of waiting until the interval has elapsed to emit, it first sends the first value then evaluates against all future values and so on. 它不是等待间隔过去才发出,而是先发送第一个值,然后根据所有未来值进行评估,依此类推。

Does an operator like this already exist? 像这样的运营商是否已经存在? And if not, how would I go about making one? 如果没有,我将如何制作一个?

You can achieve this relatively easily using the timeInterval operator which computes precisely the time interval between successive values. 您可以使用timeInterval运算符相对容易地实现此目的,该运算符精确计算连续值之间的时间间隔。 Here is a sample code you can adapt to your guise. 这是一个示例代码,您可以适应您的伪装。

http://jsfiddle.net/a7uusL6t/ http://jsfiddle.net/a7uusL6t/

var Xms = 500;
var click$ = Rx.Observable.fromEvent(document, 'click').timeInterval()
var firstClick$ = click$.first().map(function(x){return x.value});

var res$ = firstClick$
  .concat(click$
    .filter(function (x) {
      console.log('interval', x.interval);
      return x.interval > Xms;})
    .map(function(x){return x.value})
   );

res$.subscribe(function(x){console.log(x)})

I solved my problem with something similar but more refined than @user3743222's answer: 我用类似的东西解决了我的问题,但比@ user3743222的答案更精致:

const events = Rx.Observable.fromEvent(window, 'mousewheel', (e) => e.deltaY);
const firstEventObservable = events.take(1);
const remainingEventsObservable = events.skip(1)
    .timeInterval()
    .filter(x => x.interval >= this.props.delayDuration)
    .map(x => x.value);
const pageChangeObservable = firstEventObservable.concat(remainingEventsObservable);

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

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