2

I have an observable that is emitting items quite fast. I want to limit those items. My operator should only emit 1 element maximum per x seconds, ignoring the rest.

Concretely, I would like to create an operator like this:

enter image description here

As you can appreciate, it will only emit the first item, ignoring the rest for 5 time units. A, B and C are on t=1, t=2 and t=3 respectively. B and C are ignored, because the resulting sequence should emit a maximum of 1 items each 5 time units.

When D comes, it's emitted because it comes when 5 time units are due, but E is not, because D has been emitted. Finally, the original sequence doesn't emit anything for a long time, until F comes. The derived sequence emits D.

Notice that the derived sequence emits the first element, and it won't emit any other item until the specified time span has passed. Once the time span is due, it's ready to accept another item.

How can I create this operator?

halfer
  • 19,824
  • 17
  • 99
  • 186
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • Should `E` be emitted before `F` when the timer interval has been reached before `F` has been emitted (yet)? – Progman Sep 15 '19 at 17:40
  • @Progman When `E` arrives,it does inside the slot that that started with `D`. This slot ends 5 time units after `D` comes. So `E` is ignored. Another case would have been If `E` arrived a bit later, starting its own time slot. In my marble diagram, when `F` arrives, there's not active time slot, so it's emitted immediately. – SuperJMN Sep 15 '19 at 17:58
  • To clarify, the goal is to create an operator that doesn't allow more than 1 item per x seconds. It would act like doorbell filter, when you don't want people to ring it like crazy :) – SuperJMN Sep 15 '19 at 18:02
  • I've found a better example: it would act like an automatic doorbell. When you push it, it will play a sound for x seconds. If you push the button repeatedly it will ignore the presses until the sound finished. – SuperJMN Sep 15 '19 at 18:09
  • 1
    I think you will find an answer here: https://github.com/dotnet/reactive/issues/395 – Vitalii Ilchenko Sep 15 '19 at 22:18

0 Answers0