{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Reactive.Banana.Prim.IO where
import Control.Monad.IO.Class
import Data.Functor
import Data.IORef
import qualified Data.Vault.Lazy as Lazy
import Reactive.Banana.Prim.Combinators (mapP)
import Reactive.Banana.Prim.Evaluation (step)
import Reactive.Banana.Prim.Plumbing
import Reactive.Banana.Prim.Types
import Reactive.Banana.Prim.Util
debug s = id
newInput :: forall a. Build (Pulse a, a -> Step)
newInput = mdo
always <- alwaysP
key <- liftIO $ Lazy.newKey
pulse <- liftIO $ newRef $ Pulse
{ _keyP = key
, _seenP = agesAgo
, _evalP = readPulseP pulse
, _childrenP = []
, _parentsP = []
, _levelP = ground
, _nameP = "newInput"
}
let run :: a -> Step
run a = step ([P pulse, P always], Lazy.insert key (Just a) Lazy.empty)
return (pulse, run)
addHandler :: Pulse (Future a) -> (a -> IO ()) -> Build ()
addHandler p1 f = do
p2 <- mapP (fmap f) p1
addOutput p2
readLatch :: Latch a -> Build a
readLatch = readLatchB