Does Bacon.js have some combinator that allows you to collect two events emitted at the same time?

StackOverflow https://stackoverflow.com/questions/19097017

  •  29-06-2022
  •  | 
  •  

Frage

As the title says, I am looking for some combinator collect that collects events emitted at the same time into a list, similar to the one found in Reactive-Banana. So in other words:

collect :: EventStream a -> EventStream [a]
collect [(time1, e1), (time1, e2)] = [(time1, [e1,e2])]

If it does not exist already, how would I implement it? Perusing the source, I don't see some way to read the "time" of an event's occurrence, for example Bacon.Event class does not seem to record the time of its own occurrence? Should I just use Javascript's native new Date().getTime() function to mark events ex post facto, and assert events happening within some arbitrary time frame are in fact "simultaneous".

War es hilfreich?

Lösung

There's no notion of event simultaneity in Bacon.js. Neither do the events carry a timestamp.

You can group roughly simultaneous events using stream.bufferWithTime(1). You'll get a stream of arrays of roughly simultaneous events.

If you need the time in the output, you can use stream.map(f) with a function that combines the array with the current time.

So, something like this:

stream.bufferWithTime(1).map(function(events) {
  return {
    events: events,
    timestamp: new Date().getTime()
  }
})
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top