@@ -34,11 +34,11 @@ module Control.AutoUpdate (
34 , defaultUpdateSettings
34 , defaultUpdateSettings
35 -- * Accessors
35 -- * Accessors
36 , updateAction
36 , updateAction
38 , updateFreq
37 , updateFreq
39 , updateSpawnThreshold
38 , updateSpawnThreshold
40 -- * Creation
39 -- * Creation
41 , mkAutoUpdate
40 , mkAutoUpdate
41+ , mkAutoUpdateWithModify
44#if __GLASGOW_HASKELL__ < 709
44#if __GLASGOW_HASKELL__ < 709
@@ -60,7 +60,6 @@ defaultUpdateSettings = UpdateSettings
60 { updateFreq = 1000000
60 { updateFreq = 1000000
61 , updateSpawnThreshold = 3
61 , updateSpawnThreshold = 3
62 , updateAction = return ()
62 , updateAction = return ()
63− , updateActionModify = Nothing
66-- | Settings to control how values are updated.
65-- | Settings to control how values are updated.
@@ -97,19 +96,20 @@ data UpdateSettings a = UpdateSettings
97 -- Default: does nothing.
96 -- Default: does nothing.
99 -- @since 0.1.0
98 -- @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.
104− -- Default: does nothing.
101+mkAutoUpdate :: UpdateSettings a -> IO (IO a)
102+mkAutoUpdate us = mkAutoUpdateHelper us Nothing
104+mkAutoUpdateWithModify :: UpdateSettings a -> (a -> IO a) -> IO (IO a)
105+mkAutoUpdateWithModify us f = mkAutoUpdateHelper us (Just f)
107-- | Generate an action which will either read from an automatically
107-- | Generate an action which will either read from an automatically
108-- updated value, or run the update action in the current thread.
108-- updated value, or run the update action in the current thread.
110-- @since 0.1.0
110-- @since 0.1.0
111−mkAutoUpdate :: UpdateSettings a -> IO (IO a)
111+mkAutoUpdateHelper :: UpdateSettings a -> Maybe (a -> IO a) -> IO (IO a)
112−mkAutoUpdate us = do
112+mkAutoUpdateHelper us updateActionModify = do
113 -- A baton to tell the worker thread to generate a new value.
113 -- A baton to tell the worker thread to generate a new value.
114 needsRunning <- newEmptyMVar
114 needsRunning <- newEmptyMVar
@@ -157,7 +157,7 @@ mkAutoUpdate us = do
157 takeMVar needsRunning
157 takeMVar needsRunning
159 -- new value requested, so run the updateAction
159 -- 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)
162 -- we got a new value, update currRef and lastValue
162 -- we got a new value, update currRef and lastValue
163 writeIORef currRef $ Right a
163 writeIORef currRef $ Right a