I'm writing a music player in Haskell with reactive-banana. One problem I have is fetching up-to-date values with fromPoll. I want to enable the user to optionally select a part of the track while playing. My code looks something like this:
makePlayNetworkDescr :: Player a => AddHandler Command -> a -> NetworkDescription t ()
makePlayNetworkDescr addCmdEvent player = do
bPosition <- fromPoll (getPosition player)
eCmds <- fromAddHandler addCmdEvent
let eSetStart = filterE (isJust) $ bPosition <@ filterE (==SetStart) eCmds
eSetEnd = filterE (isJust) $ bPosition <@ filterE (==SetEnd) eCmds
eClearRange = filterE (==ClearRange) eCmds
bStart = accumB Nothing ((const <$> eSetStart) `union` (const Nothing <$ eClearRange))
bEnd = accumB Nothing ((const <$> eSetEnd) `union` (const Nothing <$ eClearRange))
Above, getPosition is a partial function, returning Nothing before the playback actually starts. The problem is that once the addCmdEvent fires for the first time, bPosition will still hold a Nothing value. eSetStart/End calculate their values based on this. Only then does bPosition get updated, and this is the value which will be used next time that addCmdEvent fires. And so on, the value will always be "off by one", so to speak.
There is a related SO question, but in that case there exists a "trigger" event which can be used to calculate the new value of the behavior. Is anything like that possible with fromPoll?