Skip to content

Commit

Permalink
Adds React tool
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-k committed Nov 11, 2023
1 parent 8d4dd4a commit 44ec395
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mig-tools/src/Mig/Tool/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Mig.Tool.Base (
filterSet,
GetOr (..),
Proc (..),
React (..),
filterReact,
module X,
) where

Expand Down Expand Up @@ -78,3 +80,29 @@ instance Semigroup Proc where

instance Monoid Proc where
mempty = Proc (pure ())

{-| Process that runs forked background process which accepts a callback.
It returns a procedure to close the process.
-}
newtype React a = React
{react :: Set a -> IO Proc}

instance Functor React where
fmap f (React a) = React (a . contramap f)

filterReact :: (a -> Bool) -> React a -> React a
filterReact f (React a) = React (a . filterSet f)

accumReact :: (b -> a -> b) -> b -> React a -> React b
accumReact go initVal (React x) = React $ \call -> do
ref <- newIORef initVal
call

instance Semigroup (React a) where
(<>) (React a) (React b) = React $ \f -> do
finA <- a f
finB <- b f
pure (finA <> finB)

instance Monoid (React a) where
mempty = React (const $ pure mempty)

0 comments on commit 44ec395

Please sign in to comment.