Skip to content

Commit

Permalink
Untangle the refresh function
Browse files Browse the repository at this point in the history
The purpose of this commit is to make the `update` function a bit easier
to read and use. We do this by:

- Introducing `createViewReference` which creates an `IORef` to a `View`.
- Moving the parts of the code that creates a view reference from
  `refresh` to `createViewReference`, thus giving it one less concern.
- The `refresh` function now takes a view reference and an update
  semaphor/lock/whatever it is as arguments and when the lock is
  unlocked it runs the `update` function. This is much easier to read
  and understand than having `refresh` returning a callback, which it
  did before.

This change does not throw any shade on any of the earlier developers of
this project, it only recognizes that I'm becoming less clever for each
passing day.
  • Loading branch information
Rembane committed Sep 27, 2023
1 parent 2f723e0 commit 2122470
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
25 changes: 11 additions & 14 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Main
) where

import Control.Concurrent ( MVar
, newMVar
, newEmptyMVar
, threadDelay
, tryPutMVar
)
Expand Down Expand Up @@ -72,11 +72,9 @@ main = (reifyConfig . getOpt Permute opts <$> getArgs) >>= \case
(_ , _ : _, _ ) -> usage
(Config { _cHelp = True }, _ , _ ) -> usage
(config , _ , _ ) -> do
upd <- newMVar () -- putMVar when to update
mgr <- newTlsManager
(viewRef, refreshAction) <- runLoggingT
(runReaderT refresh (ClientContext config mgr))
print
upd <- newEmptyMVar -- putMVar when to update
mgr <- newTlsManager
viewRef <- createViewReference

-- In the list there are three items running concurrently:
-- 1. Timer that sends a signal to the updater when it's time to update
Expand All @@ -86,23 +84,22 @@ main = (reifyConfig . getOpt Permute opts <$> getArgs) >>= \case
Async.Concurrently
[ timer upd config
, webserver config viewRef upd
, updater mgr upd refreshAction config
, updater mgr upd viewRef config
]
where
timer upd cfg =
forever $ tryPutMVar upd () >> threadDelay (view cInterval cfg)

updater mgr upd refreshAction cfg =
updater mgr upd viewRef cfg =
forever
$ withFDHandler defaultBatchingOptions stdout 1.0 80
$ \logCallback -> runLoggingT
(runReaderT (refreshAction upd) (ClientContext cfg mgr))
(runReaderT (refresh viewRef upd) (ClientContext cfg mgr))
( logCallback
. renderWithTimestamp
(formatTime defaultTimeLocale
(iso8601DateFormat (Just "%H:%M:%S"))
)
id
. renderWithTimestamp (
formatTime defaultTimeLocale (iso8601DateFormat (Just "%H:%M:%S"))
)
id
)

webserver
Expand Down
30 changes: 14 additions & 16 deletions src/Model.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{-# LANGUAGE FlexibleContexts, OverloadedStrings #-}
module Model
( refresh
, Restaurant(..)
( Restaurant(..)
, Menu(..)
, View(..)
, createViewReference
, refresh
)
where

import Control.Arrow ( (>>>) )
import Control.Concurrent.MVar ( MVar
, takeMVar
)
Expand All @@ -23,7 +23,6 @@ import Control.Monad.Log ( MonadLog
import Control.Monad.Reader ( MonadReader
, asks
)
import Data.Functor ( (<&>) )
import Data.IORef ( IORef
, newIORef
, writeIORef
Expand Down Expand Up @@ -64,18 +63,17 @@ refresh
, MonadReader ClientContext m
, MonadThrow m
)
=> m (IORef View, MVar () -> m ())
refresh
= liftIO
(getZonedTime >>= (view _zonedTimeToLocalTime >>> newIORef . View [] ""))
<&> \ref ->
( ref
, \upd -> do
liftIO $ takeMVar upd
logMessage =<< timestamp "Updating view..."
v <- update
liftIO $ writeIORef ref v
)
=> IORef View -> MVar () -> m ()
refresh ref upd = do
liftIO $ takeMVar upd
logMessage =<< timestamp "Updating view..."
v <- update
liftIO $ writeIORef ref v

createViewReference :: (MonadIO m) => m (IORef View)
createViewReference = liftIO $ do
now <- getZonedTime
newIORef (View [] "" (now ^. _zonedTimeToLocalTime))

update
:: ( MonadIO m
Expand Down

0 comments on commit 2122470

Please sign in to comment.