module Control.Observable
( Observable, dispatch, obs
, notify, chase, subscribe, watch
, (.:~.), (.:~*), (*:~.), (*:~*)
) where
import "base" Control.Applicative (Applicative (pure))
import "base" Control.Monad (Monad, forever)
import "base" Data.Function (($), (.), flip)
import "base" Data.Traversable (Traversable (traverse))
import "transformers" Control.Monad.Trans.Cont (ContT (..))
import "transformers" Control.Monad.Trans.Class (lift)
newtype Capture r f a = Capture { captured :: f r }
type Observable f a r = ContT r (Capture r f) a
dispatch :: ContT r f a -> Observable f a r
dispatch f = ContT $ \h -> Capture $ runContT f (captured . h)
obs :: Monad f => f a -> Observable f a r
obs action = dispatch $ lift action
notify :: Observable f a r -> (a -> f r) -> f r
notify r f = captured $ runContT r (Capture . f)
(.:~.) :: Observable f a r -> (a -> f r) -> f r
(.:~.) = notify
chase :: Applicative f => Observable f a r -> (a -> f r) -> f r
chase r f = captured $ runContT r (Capture . forever . f)
(.:~*) :: Applicative f => Observable f a r -> (a -> f r) -> f r
(.:~*) = chase
subscribe :: Applicative f => Observable f a r -> (a -> f r) -> f r
subscribe r f = forever $ captured $ runContT r (Capture . f)
(*:~.) :: Applicative f => Observable f a r -> (a -> f r) -> f r
(*:~.) = subscribe
watch :: Applicative f => Observable f a r -> (a -> f r) -> f r
watch r f = forever $ captured $ runContT r (Capture . forever . f)
(*:~*) :: Applicative f => Observable f a r -> (a -> f r) -> f r
(*:~*) = watch