on develop help sign in
Add Reflex.Dom.Time
commitd94c9f2beb56d7f4f128a22995cec8069fcc05f3 authorRyan Trinkle <ryan.trinkle@gmail.com> dateMon 13 Apr 2015, 21:27 (12 years ago) parent4711500
3 files changed+31−3
reflex-dom.cabal+4−2 src/Reflex/Dom.hs+5−1 src/Reflex/Dom/Time.hs+22
reflex-dom.cabal+4−2
@@ -29,8 +29,9 @@ library
2929 bytestring >=0.10.6.0 && <0.11,
3030 data-default >=0.5.3 && <0.6,
3131 semigroups >=0.16.1 && <0.17,
32− ref-tf,
33− aeson >=0.8.0.2 && <0.9
32+ ref-tf == 0.4.*,
33+ aeson >=0.8.0.2 && <0.9,
34+ time >=1.5.0.1 && <1.6
3435
3536 if impl(ghcjs)
3637 hs-source-dirs: src-ghcjs
@@ -54,6 +55,7 @@ library
5455 Reflex.Dom.Widget.Input
5556 Reflex.Dom.Widget.Lazy
5657 Reflex.Dom.Xhr
58+ Reflex.Dom.Time
5759 other-modules:
5860 Reflex.Dom.Internal.Foreign
5961 Reflex.Dom.Xhr.Foreign
src/Reflex/Dom.hs+5−1
@@ -1,10 +1,14 @@
1−module Reflex.Dom ( module Reflex.Dom.Class
1+module Reflex.Dom ( module Reflex
2+ , module Reflex.Dom.Class
23 , module Reflex.Dom.Internal
34 , module Reflex.Dom.Widget
45 , module Reflex.Dom.Xhr
6+ , module Reflex.Dom.Time
57 ) where
68
9+import Reflex
710import Reflex.Dom.Class
811import Reflex.Dom.Internal
912import Reflex.Dom.Widget
1013import Reflex.Dom.Xhr
14+import Reflex.Dom.Time
src/Reflex/Dom/Time.hsnew file+22
@@ -0,0 +1,22 @@
1+module Reflex.Dom.Time where
2+
3+import Reflex
4+import Reflex.Dom.Class
5+
6+import Control.Concurrent
7+import Control.Monad
8+import Control.Monad.IO.Class
9+import Data.Fixed
10+import Data.Time.Clock
11+
12+-- | Send events over time with the given basis time and interval
13+-- If the system starts running behind, occurrences will be dropped rather than buffered
14+-- Each occurrence of the resulting event will contain the index of the current interval, with 0 representing the basis time
15+skippableIntervalsAround :: MonadWidget t m => UTCTime -> NominalDiffTime -> m (Event t Integer)
16+skippableIntervalsAround t0 dt = performEventAsync . fmap callAtNextInterval =<< getPostBuild
17+ where callAtNextInterval _ cb = void $ liftIO $ forkIO $ do
18+ t <- getCurrentTime
19+ let offset = t `diffUTCTime` t0
20+ (n, delay) = offset `divMod'` dt
21+ threadDelay $ ceiling $ delay * 1000000
22+ cb n