on cd85893 help sign in
49 lines 2.5 kB history raw
1-- | This module defines 'TriggerEvent', which describes actions that may create
2-- new 'Event's that can be triggered from 'IO'.
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FunctionalDependencies #-}
5{-# LANGUAGE UndecidableInstances #-}
6module Reflex.TriggerEvent.Class
7 ( TriggerEvent (..)
8 ) where
10import Reflex.Class
12import Control.Monad.Reader
13import Control.Monad.State
14import qualified Control.Monad.State.Strict as Strict
16--TODO: Shouldn't have IO hard-coded
17-- | 'TriggerEvent' represents actions that can create 'Event's that can be
18-- triggered by 'IO' actions.
19class Monad m => TriggerEvent t m | m -> t where
20 -- | Create a triggerable 'Event'. Whenever the resulting function is called,
21 -- the resulting 'Event' will fire at some point in the future. Note that
22 -- this may not be synchronous.
23 newTriggerEvent :: m (Event t a, a -> IO ())
24 -- | Like 'newTriggerEvent', but the callback itself takes another callback,
25 -- to be invoked once the requested 'Event' occurrence has finished firing.
26 -- This allows synchronous operation.
27 newTriggerEventWithOnComplete :: m (Event t a, a -> IO () -> IO ()) --TODO: This and newTriggerEvent should be unified somehow
28 -- | Like 'newTriggerEventWithOnComplete', but with setup and teardown. This
29 -- relatively complex type signature allows any external listeners to be
30 -- subscribed lazily and then removed whenever the returned 'Event' is no
31 -- longer being listened to. Note that the setup/teardown may happen multiple
32 -- times, and there is no guarantee that the teardown will be executed
33 -- promptly, or even at all, in the case of program termination.
34 newEventWithLazyTriggerWithOnComplete :: ((a -> IO () -> IO ()) -> IO (IO ())) -> m (Event t a)
36instance TriggerEvent t m => TriggerEvent t (ReaderT r m) where
37 newTriggerEvent = lift newTriggerEvent
38 newTriggerEventWithOnComplete = lift newTriggerEventWithOnComplete
39 newEventWithLazyTriggerWithOnComplete = lift . newEventWithLazyTriggerWithOnComplete
41instance TriggerEvent t m => TriggerEvent t (StateT s m) where
42 newTriggerEvent = lift newTriggerEvent
43 newTriggerEventWithOnComplete = lift newTriggerEventWithOnComplete
44 newEventWithLazyTriggerWithOnComplete = lift . newEventWithLazyTriggerWithOnComplete
46instance TriggerEvent t m => TriggerEvent t (Strict.StateT s m) where
47 newTriggerEvent = lift newTriggerEvent
48 newTriggerEventWithOnComplete = lift newTriggerEventWithOnComplete
49 newEventWithLazyTriggerWithOnComplete = lift . newEventWithLazyTriggerWithOnComplete