3

Here's an interesting one. I'm working with FRP, and looking at the 'debounce' methods in various libraries (jQuery, Bacon.js). I started working with this, and found it almost does what I need, but with a subtle difference. Here's my understanding of debounce as it relates to event handling:

When an event is happening with at least a certain frequency, don't do anything. Once the events slow down to less than the frequency, fire the event handler. For example (pseudo-code) for key-presses.debounce(1 second) then Alert, we would see nothing happen if keys are pressed within a second of each other, until 1 second after the last key was pressed, then we'd get an Alert.

What I need is something that fires at the start of the sequence, not after the end. So, for the same example, we'd see the Alert immediately, then nothing. If the user starts pressing keys again after at least 1 second, we'd get another alert, then nothing again.

The code is easy - I just want a name for this. It still takes a single argument for the frequency: BeginSequence(1000)? AfterExpiry(1000)?

Any suggestions?

Ben Hull
  • 7,524
  • 3
  • 36
  • 56
  • 1
    I'd call it a "rate limiter" or "throttle" although, perhaps "prebounce" (made up, btw) is more cutesy .. maybe "PreventUntil" even. – user2246674 Apr 29 '13 at 01:17
  • How about AggregateAndDelayBy()? You could count up the number of events ignored during the period and provide that when you fire the event, even if it's not needed, as a way of better describing what's going on. – RenniePet Apr 29 '13 at 01:27
  • It's definitely not a 'throttle' operation - in that the event will fire once, then never again until the 'sequence' stops or slows to greater than the frequency. – Ben Hull Apr 29 '13 at 01:29
  • Simiarly, the 'DelayBy' side doesn't seem right, either - the difference between this and debounce is there's no delay to begin with. The delay enforced is between two sets of sequences... – Ben Hull Apr 29 '13 at 01:30
  • Prebounce is pretty good! – Ben Hull Apr 29 '13 at 01:31
  • I'd call it something like Combine because it merges a lot of temporally-nearby occurrences into one occurrence. CombineNearbyEvents? CombineUntil? – Doug McClean Apr 29 '13 at 02:09
  • Hmm - not bad, but FRP (particularly Bacon.js) already uses combine for something unrelated... – Ben Hull Apr 29 '13 at 02:11

1 Answers1

0

So you want an event when there is an event after a long enough period of no events?
I would call that event a silence-breaking event, so maybe something like breakSilence? I'm not sure that looks right, maybe onBreakSilence, but I'm not familiar with nomenclature of Bacon so I don't know if onX is used.
Another option might be listenAfter.

key-presses.onBreakSilence(1 second) then Alert

key-presses.listenAfter(1 second) then Alert

Apanatshka
  • 5,958
  • 27
  • 38