module Control.Wire.Unsafe.Event
(
Event(..),
event,
merge,
occurred,
onEventM
)
where
import Control.DeepSeq
import Control.Monad
import Control.Wire.Core
import Data.Semigroup
import Data.Typeable
data Event a = Event a | NoEvent deriving (Typeable)
instance Functor Event where
fmap f = event NoEvent (Event . f)
instance (Semigroup a) => Monoid (Event a) where
mempty = NoEvent
mappend = (<>)
instance (NFData a) => NFData (Event a) where
rnf (Event x) = rnf x
rnf NoEvent = ()
instance (Semigroup a) => Semigroup (Event a) where
(<>) = merge (<>)
event :: b -> (a -> b) -> Event a -> b
event _ j (Event x) = j x
event n _ NoEvent = n
merge :: (a -> a -> a) -> Event a -> Event a -> Event a
merge _ NoEvent NoEvent = NoEvent
merge _ (Event x) NoEvent = Event x
merge _ NoEvent (Event y) = Event y
merge f (Event x) (Event y) = Event (f x y)
occurred :: Event a -> Bool
occurred = event False (const True)
onEventM :: (Monad m) => (a -> m b) -> Wire s e m (Event a) (Event b)
onEventM c = mkGen_ $ liftM Right . event (return NoEvent) (liftM Event . c)