{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Reactive.Banana.Prim.Low.IO where
import Control.Monad.IO.Class
import qualified Data.Vault.Lazy as Lazy
import Reactive.Banana.Prim.Mid.Combinators (mapP)
import Reactive.Banana.Prim.Low.Evaluation (step)
import Reactive.Banana.Prim.Low.Plumbing
import Reactive.Banana.Prim.Low.Types
import Reactive.Banana.Prim.Low.Util
debug :: String -> a -> a
debug :: String -> a -> a
debug String
_ = a -> a
forall a. a -> a
id
newInput :: forall a. Build (Pulse a, a -> Step)
newInput :: Build (Pulse a, a -> Step)
newInput = mdo
Pulse ()
always <- Build (Pulse ())
alwaysP
Key (Maybe a)
key <- IO (Key (Maybe a))
-> ReaderWriterIOT BuildR BuildW IO (Key (Maybe a))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (Key (Maybe a))
forall a. IO (Key a)
Lazy.newKey
Pulse a
pulse <- IO (Pulse a) -> ReaderWriterIOT BuildR BuildW IO (Pulse a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Pulse a) -> ReaderWriterIOT BuildR BuildW IO (Pulse a))
-> IO (Pulse a) -> ReaderWriterIOT BuildR BuildW IO (Pulse a)
forall a b. (a -> b) -> a -> b
$ Pulse' a -> IO (Pulse a)
forall (m :: * -> *) a. MonadIO m => a -> m (Ref a)
newRef (Pulse' a -> IO (Pulse a)) -> Pulse' a -> IO (Pulse a)
forall a b. (a -> b) -> a -> b
$ Pulse :: forall a.
Key (Maybe a)
-> Time
-> EvalP (Maybe a)
-> [Weak SomeNode]
-> [Weak SomeNode]
-> Level
-> String
-> Pulse' a
Pulse
{ _keyP :: Key (Maybe a)
_keyP = Key (Maybe a)
key
, _seenP :: Time
_seenP = Time
agesAgo
, _evalP :: EvalP (Maybe a)
_evalP = Pulse a -> EvalP (Maybe a)
forall a. Pulse a -> EvalP (Maybe a)
readPulseP Pulse a
pulse
, _childrenP :: [Weak SomeNode]
_childrenP = []
, _parentsP :: [Weak SomeNode]
_parentsP = []
, _levelP :: Level
_levelP = Level
ground
, _nameP :: String
_nameP = String
"newInput"
}
let run :: a -> Step
run :: a -> Step
run a
a = Inputs -> Step
step ([Pulse a -> SomeNode
forall a. Pulse a -> SomeNode
P Pulse a
pulse, Pulse () -> SomeNode
forall a. Pulse a -> SomeNode
P Pulse ()
always], Key (Maybe a) -> Maybe a -> Vault -> Vault
forall a. Key a -> a -> Vault -> Vault
Lazy.insert Key (Maybe a)
key (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Vault
Lazy.empty)
(Pulse a, a -> Step) -> Build (Pulse a, a -> Step)
forall (m :: * -> *) a. Monad m => a -> m a
return (Pulse a
pulse, a -> Step
run)
addHandler :: Pulse (Future a) -> (a -> IO ()) -> Build ()
addHandler :: Pulse (Future a) -> (a -> IO ()) -> Build ()
addHandler Pulse (Future a)
p1 a -> IO ()
f = do
Pulse (IO (IO ()))
p2 <- (Future a -> IO (IO ()))
-> Pulse (Future a) -> Build (Pulse (IO (IO ())))
forall a b. (a -> b) -> Pulse a -> Build (Pulse b)
mapP ((a -> IO ()) -> Future a -> IO (IO ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> IO ()
f) Pulse (Future a)
p1
Pulse (IO (IO ())) -> Build ()
addOutput Pulse (IO (IO ()))
p2
readLatch :: Latch a -> Build a
readLatch :: Latch a -> Build a
readLatch = Latch a -> Build a
forall a. Latch a -> Build a
readLatchB