Skip to content
Snippets Groups Projects
Commit 85467aaf authored by Mr. Snow Ball / projects's avatar Mr. Snow Ball / projects :arrows_counterclockwise:
Browse files

Refactor qubesMonitorPropertyBlock: extract pipeBlockWithEvents

parent eb829644
No related branches found
No related tags found
No related merge requests found
...@@ -43,10 +43,10 @@ diskUsageQubesBlock = runPollBlock $ forever $ do ...@@ -43,10 +43,10 @@ diskUsageQubesBlock = runPollBlock $ forever $ do
((unit, factor) : _) -> T.pack (show $ size `div` factor) <> unit ((unit, factor) : _) -> T.pack (show $ size `div` factor) <> unit
_ -> T.pack (show size) <> " bytes" _ -> T.pack (show size) <> " bytes"
qubesMonitorPropertyBlock :: BL.ByteString -> Block pipeBlockWithEvents :: forall a. Producer a (P.SafeT IO) () -> (Either BlockEvent a -> BarIO (Maybe BlockOutput)) -> Block
qubesMonitorPropertyBlock name = do pipeBlockWithEvents prod block = do
(output, input) <- liftIO $ spawn $ newest 1 (output, input) <- liftIO $ spawn $ newest 1
forkSafeEffect $ qubesMonitorProperty qubesEvents name >-> P.map Right >-> toOutput output forkSafeEffect $ prod >-> P.map Right >-> toOutput output
toExitBlock $ fromInput input >-> forever (update output) toExitBlock $ fromInput input >-> forever (update output)
where where
forkSafeEffect :: MonadIO m => Effect (P.SafeT IO) () -> m () forkSafeEffect :: MonadIO m => Effect (P.SafeT IO) () -> m ()
...@@ -57,20 +57,25 @@ qubesMonitorPropertyBlock name = do ...@@ -57,20 +57,25 @@ qubesMonitorPropertyBlock name = do
toExitBlock = fmap (const ExitBlock) toExitBlock = fmap (const ExitBlock)
decode = decodeUtf8With lenientDecode update :: Output (Either (BlockOutput, BlockEvent) a) -> Pipe (Either (BlockOutput, BlockEvent) a) (BlockState, BlockUpdateReason) BarIO ()
update output = await >>= \case update output = await >>= \case
Right prop -> update' prop Right prop -> update' $ Right prop
Left blockOutput -> do Left (blockOutput, event) -> do
let state = Just (blockOutput, Nothing) let state = Just (blockOutput, Nothing)
yield (invalidateBlockState state, EventUpdate) yield (invalidateBlockState state, EventUpdate)
prop <- liftIO (qubesGetProperty name) update' $ Left event
update' prop
where where
update' :: Either BlockEvent a -> P.Pipe b BlockUpdate BarIO ()
update' prop = do update' prop = do
let QubesPropertyInfo {propValue, propIsDefault} = prop Just blockOutput <- lift $ block prop
let blockOutput = mkBlockOutput $ normalText $ decode propValue <> (if propIsDefault then " (D)" else "")
pushBlockUpdate' (handleClick blockOutput) blockOutput pushBlockUpdate' (handleClick blockOutput) blockOutput
handleClick blockOutput _ = do handleClick blockOutput event = do
forkEffect $ yield (Left blockOutput) >-> toOutput output forkEffect $ yield (Left (blockOutput, event)) >-> toOutput output
qubesMonitorPropertyBlock :: BL.ByteString -> Block
qubesMonitorPropertyBlock name = pipeBlockWithEvents (qubesMonitorProperty qubesEvents name) handle
where
handle = handle' <=< either (const $ liftIO $ qubesGetProperty name) return
handle' QubesPropertyInfo {propValue, propIsDefault} = return $ Just $ mkBlockOutput $ normalText $ decode propValue <> (if propIsDefault then " (D)" else "")
decode = decodeUtf8With lenientDecode
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment