on master help sign in
Prototyping non-breaking approach to udpateActionModify
commit60fbb467034d98634ada4d65b9c5b4663d8e53c8 authornanonaren <nano.naren@gmx.com> dateThu 5 May 2016, 10:42 (10 years ago) parent3ce4822
1 file changed+10−10
auto-update/Control/AutoUpdate.hs+10−10
auto-update/Control/AutoUpdate.hs+10−10
@@ -34,11 +34,11 @@ module Control.AutoUpdate (
3434 , defaultUpdateSettings
3535 -- * Accessors
3636 , updateAction
37− , updateActionModify
3837 , updateFreq
3938 , updateSpawnThreshold
4039 -- * Creation
4140 , mkAutoUpdate
41+ , mkAutoUpdateWithModify
4242 ) where
4343
4444#if __GLASGOW_HASKELL__ < 709
@@ -60,7 +60,6 @@ defaultUpdateSettings = UpdateSettings
6060 { updateFreq = 1000000
6161 , updateSpawnThreshold = 3
6262 , updateAction = return ()
63− , updateActionModify = Nothing
6463 }
6564
6665-- | Settings to control how values are updated.
@@ -97,19 +96,20 @@ data UpdateSettings a = UpdateSettings
9796 -- Default: does nothing.
9897 --
9998 -- @since 0.1.0
100− , updateActionModify :: Maybe (a -> IO a)
101− -- ^ Optional action to be performed to get the current value
102− -- and update it if necessary.
103− --
104− -- Default: does nothing.
10599 }
106100
101+mkAutoUpdate :: UpdateSettings a -> IO (IO a)
102+mkAutoUpdate us = mkAutoUpdateHelper us Nothing
103+
104+mkAutoUpdateWithModify :: UpdateSettings a -> (a -> IO a) -> IO (IO a)
105+mkAutoUpdateWithModify us f = mkAutoUpdateHelper us (Just f)
106+
107107-- | Generate an action which will either read from an automatically
108108-- updated value, or run the update action in the current thread.
109109--
110110-- @since 0.1.0
111−mkAutoUpdate :: UpdateSettings a -> IO (IO a)
112−mkAutoUpdate us = do
111+mkAutoUpdateHelper :: UpdateSettings a -> Maybe (a -> IO a) -> IO (IO a)
112+mkAutoUpdateHelper us updateActionModify = do
113113 -- A baton to tell the worker thread to generate a new value.
114114 needsRunning <- newEmptyMVar
115115
@@ -157,7 +157,7 @@ mkAutoUpdate us = do
157157 takeMVar needsRunning
158158
159159 -- new value requested, so run the updateAction
160− a <- catchSome $ maybe (updateAction us) id (updateActionModify us <*> maybea)
160+ a <- catchSome $ maybe (updateAction us) id (updateActionModify <*> maybea)
161161
162162 -- we got a new value, update currRef and lastValue
163163 writeIORef currRef $ Right a