Search code examples
haskellghcjsreflex

Representing timestamps


I would like to represent the timestamp coming from an HTMLMediaElement. Its defining characteristics are:

  • Its value is represented as a Double
  • It can be queried at any time using getCurrentTime :: IO Double (as partially applied on a given HTMLMediaElement)
  • It is potentially continuously changing (whenever the media player is playing)

My initial plan was to represent it as a Behavior t Double that re-runs the IO Double every time it is observed, but that hasn't worked out too well.

Things I've tried:

  • Using a Behavior that is prodded under the hood at a fixed frequency, as described in the workaround section of this question
  • Passing an Event t () representing the desired sampling frequency, and returning an Event t Double that holds the coinciding timestamps

I don't really like either -- the first one either couples the behaviour (sorry) too much to my specific use case (if I use the eventual sampling frequency I'll use in my app) or seems wasteful (if I use something like 1 kHz sampling when creating the Behavior just to then sample it at 60 Hz on the application end), and the second is quite inflexible if you want to do more than one thing with the timestamp at different sampling rates.


Solution

  • Right now, using an Event to explicitly sample the time (your second option) value is your best bet. We haven't yet created a way to write Behaviors that lazily poll outside resources, although that is something that I hope we'll be able to get done soon.

    Keep in mind that, with your second option, you don't necessarily need to use a specific sampling rate; instead, you can sample on-demand, and even have multiple locations doing that sampling. It's not perfect, but I hope that'll let you get the job done!