-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Monoidal, monadic and first-class events -- -- This package can be used to represent events as first-class objects -- instead of deepening callbacks and nesting callbacks. Useful to wrap -- over C callback-based libraries. @package event @version 0.1.3 -- | An Event a is an object representing an event of type -- a. You can register actions through it – see the on -- function – and detach them later on. -- -- An Event has many purposes. The one in mind when writing that -- package was to interface over C callback-based reactive system. -- Consider the following Haskell wrapper function, which is based -- on imperative style: -- --
-- -- Create a new button and register an action to launch when the button’s -- -- state changes. -- createButton :: (ButtonState -> IO ()) -> IO Button -- createButton callback = do -- -- create the button -- button <- ... -- forkIO . forever $ do -- -- launch a thread in which we can test whether the state has changed -- when stateHasChanged $ callback newState -- pure button ---- -- We can enhance that by representing the action of registering to the -- event and detaching from it by immediately returning a value: -- --
-- createButton :: IO (Button,Event ButtonState) -- createButton = do -- -- create the button -- button <- ... -- -- create an Event -- (ev,t) <- newEvent -- forkIO . forever $ -- -- check the new state -- when stateHasChanged $ trigger t newState -- pure (button,ev) ---- -- The Trigger can also be returned to manually invoke the -- Event. module Control.Concurrent.Event -- | An Event a is a value of type a with no -- direct representation. It lives in the future. It’s possible to -- register actions with on to execute when data becomes -- available, and to detach those actions with the resulting -- Detach object by calling detach on it. -- -- Events can be triggered with the trigger function and -- the associated type Trigger. data Event a -- | Detach is used to detach an action from an Event. newtype Detach Detach :: IO () -> Detach [detach] :: Detach -> IO () -- | Register an action. on :: (MonadIO m) => Event a -> (a -> IO ()) -> m Detach -- | Create a new Event a along with a Trigger -- a. newEvent :: (MonadIO m) => m (Event a, Trigger a) -- | Trigger a is used to trigger an -- Event a. newtype Trigger a Trigger :: (a -> IO ()) -> Trigger a -- | Use a Trigger. trigger :: (MonadIO m) => Trigger a -> a -> m () -- | Filter an Event with a predicate. filterE :: (a -> Bool) -> Event a -> Event a -- | Right fold an Event. Each time an event occur, the function -- folding function is applied and the result is passed to the future -- Event. foldrE :: (b -> a -> b) -> b -> Event a -> Event b instance GHC.Base.Applicative Control.Concurrent.Event.Event instance GHC.Base.Functor Control.Concurrent.Event.Event instance GHC.Base.Monad Control.Concurrent.Event.Event instance GHC.Base.Monoid (Control.Concurrent.Event.Event a) instance Data.Semigroup.Semigroup (Control.Concurrent.Event.Event a) instance GHC.Base.Monoid Control.Concurrent.Event.Detach instance Data.Semigroup.Semigroup Control.Concurrent.Event.Detach instance GHC.Base.Monoid (Control.Concurrent.Event.Trigger a) instance Data.Semigroup.Semigroup (Control.Concurrent.Event.Trigger a)