0

I'm trying to use a combination of purescript-pipes, and purescript-pipes-aff to write a version of await which will return Nothing if it doesn't receive a value in a specified amount of time.

So far, I've written the following:

timeOut :: forall a b. Milliseconds -> Pipe a b Aff (Maybe a)
timeOut t = do
  av <- liftAff  AVar.empty
  chan <- liftAff (spawn $ new)
  liftEffect $ launchAff_  do
      vv <- map join $ sequential $ oneOf
              [ parallel $ Nothing <$ (delay t)
              , parallel $ (Just <$> recv chan)
              ]
      AVar.put vv av
      seal chan
  P.take 1 >-> toOutput chan
  liftAff $ AVar.read av 

This nearly works, however when it times out, it still waits for the next input, and consumes it, before returning. This makes sense based on the definition of toOutput, which involves calling await.


I'm not 100% sure that it's possible to write such a function, using the pipes streaming model, but I'd love to have this confirmed (or disproved)

Joe
  • 1,479
  • 13
  • 22
  • 2
    You might want to translate this into Haskell and ask the question that way. It looks like I'm almost the only person here answering PureScript questions, and I have absolutely no familiarity with that library. I will get around to looking at it eventually, but it will probably take quite a while. – Fyodor Soikin Sep 16 '22 at 04:17
  • I'm wary that'd wind out fairly similar to (although subtly distinct from) [this question](https://stackoverflow.com/questions/52540672/how-would-i-pipe-with-a-timeout-that-resets-with-each-incoming); so I'll probably try and adapt the aproach in that rather than asking a new question – Joe Sep 16 '22 at 10:36

0 Answers0