Simplify channelHassEval now that the writer owns the flag decision

This commit is contained in:
2026-09-30 08:56:07 +03:00
parent c2425e8fb0
commit 526710ef71
2 changed files with 11 additions and 14 deletions
+8 -12
View File
@@ -1,5 +1,6 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module HomeAssistant.Runtime.Bus
( Bus(..)
@@ -31,10 +32,8 @@ import System.IO (stdout)
import System.Metrics.Counter (inc)
import Data.UUID (toText)
import AFRP (Request(..), Event(..))
import Control.Monad (when)
import Control.Monad.IO.Class (MonadIO, liftIO)
import HomeAssistant.Runtime.Flags (Flags, isEnabled)
import Data.Foldable (forM_)
import HomeAssistant.Runtime.Flags (Flags)
-- | Shared runtime state: inbound is a broadcast channel (controllers
-- read from 'dupTChan' copies), outbound queues service calls for the
@@ -71,19 +70,16 @@ recordInbound bus = inc (amTriggersIn (busMetrics bus))
recordOutbound :: Bus -> IO ()
recordOutbound bus = inc (amServicesOut (busMetrics bus))
channelHassEval :: (MonadIO m, KatipContext m) => Bus -> HASSEff a -> m a
channelHassEval :: forall m a. (MonadIO m, KatipContext m) => Bus -> HASSEff a -> m a
channelHassEval bus = \case
CallService req svc -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $ do
logFM DebugS (ls $ show svc)
enabled <- isEnabled (busFlags bus) (requestHandler req)
when enabled $ liftIO $ atomically $ writeTChan (busOutbound bus) (req, svc)
CallServices req svcs -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $ forM_ svcs $ \svc -> do
logFM DebugS (ls $ show svc)
enabled <- isEnabled (busFlags bus) (requestHandler req)
when enabled $ liftIO $ atomically $ writeTChan (busOutbound bus) (req, svc)
CallService req svc -> emit req svc
CallServices req svcs -> mapM_ (emit req) svcs
Debug x -> logFM DebugS (ls $ show x)
Trace req x -> katipAddContext (sl "traceId" (toText (requestTraceId req))) $
logFM InfoS (ls $ show x)
where
emit :: Request -> Service -> m ()
emit req svc = liftIO $ atomically $ writeTChan (busOutbound bus) (req, svc)
newtype CallIdGen = CallIdGen { generateCallId :: IO Int }