<div dir="ltr"><div class="gmail-ajy" tabindex="0"><img class="gmail-ajz" id="gmail-:1mh" src="https://mail.google.com/mail/u/0/images/cleardot.gif" alt=""></div><div>You have the auto-update package that provide that kind of functionnality</div><a href="https://hackage.haskell.org/package/auto-update-0.1.4/docs/Control-AutoUpdate.html" target="_blank">https://hackage.haskell.org/<wbr>package/auto-update-0.1.4/<wbr>docs/Control-AutoUpdate.html</a></div><div class="gmail_extra"><br><div class="gmail_quote">2017-11-24 21:53 GMT+01:00 Tristan Seligmann <span dir="ltr"><<a href="mailto:mithrandi@mithrandi.net" target="_blank">mithrandi@mithrandi.net</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I just wrote this helper:<div><br></div><div><div><font face="monospace">> import Control.Concurrent</font></div><div><font face="monospace">> import Control.Concurrent.STM</font></div><div><font face="monospace">> import Control.Exception.Safe</font></div><div><font face="monospace">> </font></div><div><font face="monospace">> pollT :: Int -> IO a -> IO (STM (Maybe a), Async b)<br></font></div><div><font face="monospace">> pollT delay act = do</font></div><div><font face="monospace">> tv <- atomically (newTVar Nothing)</font></div><div><font face="monospace">> as <-</font></div><div><font face="monospace">> async . forever $ do</font></div><div><font face="monospace">> r <- tryAny act</font></div><div><font face="monospace">> case r of</font></div><div><font face="monospace">> Left _ -> pure ()</font></div><div><font face="monospace">> Right r' -> atomically (writeTVar tv (Just r'))</font></div><div><font face="monospace">> threadDelay delay</font></div><div><font face="monospace">> pure (readTVar tv, as)</font></div></div><div><br></div><div>I was sort of surprised not to find something like this in an existing library.</div><div><ol><li>Did I miss an existing implementation?</li><li>Any problems with this one?</li><li>Any suggestions for a better name?</li><li>Any thoughts on the general idea? That is, "run an action periodically, updating a TVar with the result".</li></ol><div>There's a couple of obvious variations that can be built on top of this, like retrying if the TVar is Nothing when reading from it, or writing Nothing to the TVar when the action fails rather than keeping the old value. Maybe passing in the old value to the action?</div></div></div>
<br>______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div><br></div>