on master help sign in
Cleanup
1b3911bnanonaren
auto-update/Control/AutoUpdate.hs+10−7
@@ -6,12 +6,12 @@
6-- second, and write the current time to a shared 'IORef', than it is for each
6-- second, and write the current time to a shared 'IORef', than it is for each
7-- request to make its own call to 'getCurrentTime'.
7-- request to make its own call to 'getCurrentTime'.
8--
8--
9−-- But for a low-volume server, whose request frequency is less than once per
9+-- But for a low-volume server, whose request frequency is less than once per
10−-- second, that approach will result in /more/ calls to 'getCurrentTime' than
10+-- second, that approach will result in /more/ calls to 'getCurrentTime' than
11-- necessary, and worse, kills idle GC.
11-- necessary, and worse, kills idle GC.
12--
12--
13-- This library solves that problem by allowing you to define actions which will
13-- This library solves that problem by allowing you to define actions which will
14−-- either be performed by a dedicated thread, or, in times of low volume, will
14+-- either be performed by a dedicated thread, or, in times of low volume, will
15-- be executed by the calling thread.
15-- be executed by the calling thread.
16--
16--
17-- Example usage:
17-- Example usage:
@@ -98,16 +98,19 @@ data UpdateSettings a = UpdateSettings
98 -- @since 0.1.0
98 -- @since 0.1.0
99 }
99 }
100
100
101+-- | Generate an action which will either read from an automatically
102+-- updated value, or run the update action in the current thread.
103+--
104+-- @since 0.1.0
101mkAutoUpdate :: UpdateSettings a -> IO (IO a)
105mkAutoUpdate :: UpdateSettings a -> IO (IO a)
102mkAutoUpdate us = mkAutoUpdateHelper us Nothing
106mkAutoUpdate us = mkAutoUpdateHelper us Nothing
103
107
108+-- | Generate an action which will either read from an automatically
109+-- updated value, or run the update action in the current thread if
110+-- the first time or the provided modify action after that.
104mkAutoUpdateWithModify :: UpdateSettings a -> (a -> IO a) -> IO (IO a)
111mkAutoUpdateWithModify :: UpdateSettings a -> (a -> IO a) -> IO (IO a)
105mkAutoUpdateWithModify us f = mkAutoUpdateHelper us (Just f)
112mkAutoUpdateWithModify us f = mkAutoUpdateHelper us (Just f)
106
113
107−-- | Generate an action which will either read from an automatically
108−-- updated value, or run the update action in the current thread.
109−--
110−-- @since 0.1.0
111mkAutoUpdateHelper :: UpdateSettings a -> Maybe (a -> IO a) -> IO (IO a)
114mkAutoUpdateHelper :: UpdateSettings a -> Maybe (a -> IO a) -> IO (IO a)
112mkAutoUpdateHelper us updateActionModify = do
115mkAutoUpdateHelper us updateActionModify = do
113 -- A baton to tell the worker thread to generate a new value.
116 -- A baton to tell the worker thread to generate a new value.