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)