module Control.Wire.Interval
(
inhibit,
after,
for,
unless,
when,
asSoonAs,
between,
hold,
holdFor,
until
)
where
import Control.Arrow
import Control.Wire.Core
import Control.Wire.Event
import Control.Wire.Session
import Control.Wire.Unsafe.Event
import Data.Monoid
import Prelude hiding (until)
after :: (HasTime t s, Monoid e) => t -> Wire s e m a a
after t' =
mkPure $ \ds x ->
let t = t' dtime ds in
if t <= 0
then (Right x, mkId)
else (Left mempty, after t)
asSoonAs :: (Monoid e) => Wire s e m (Event a) a
asSoonAs = hold
between :: (Monoid e) => Wire s e m (a, Event b, Event c) a
between =
mkPureN $ \(x, onEv, _) ->
event (Left mempty, between)
(const (Right x, active))
onEv
where
active =
mkPureN $ \(x, _, offEv) ->
event (Right x, active)
(const (Left mempty, between))
offEv
for :: (HasTime t s, Monoid e) => t -> Wire s e m a a
for t' =
mkPure $ \ds x ->
let t = t' dtime ds in
if t <= 0
then (Left mempty, mkEmpty)
else (Right x, for t)
hold :: (Monoid e) => Wire s e m (Event a) a
hold =
mkPureN $
event (Left mempty, hold)
(Right &&& holdWith)
where
holdWith x =
mkPureN $
event (Right x, holdWith x)
(Right &&& holdWith)
holdFor :: (HasTime t s, Monoid e) => t -> Wire s e m (Event a) a
holdFor int | int <= 0 = error "holdFor: Non-positive interval."
holdFor int = off
where
off =
mkPure $ \_ ->
event (Left mempty, off)
(Right &&& on int)
on t' x' =
mkPure $ \ds ->
let t = t' dtime ds in
event (if t <= 0
then (Left mempty, off)
else (Right x', on t x'))
(Right &&& on int)
inhibit :: e -> Wire s e m a b
inhibit = mkConst . Left
unless :: (Monoid e) => (a -> Bool) -> Wire s e m a a
unless p =
mkPure_ $ \x ->
if p x then Left mempty else Right x
until :: (Monoid e) => Wire s e m (a, Event b) a
until =
mkPureN . uncurry $ \x ->
event (Right x, until) (const (Left mempty, mkEmpty))
when :: (Monoid e) => (a -> Bool) -> Wire s e m a a
when p =
mkPure_ $ \x ->
if p x then Right x else Left mempty