module Updater (
Signal(),
newSignal,
newSignalIO,
writeSignal,
readSignal,
Updater(),
runUpdater,
getEvent,
onCommit,
onCleanup,
stop,
modifySignal,
getBehavior,
local,
liftSTM,
putLine,
runGlobalUpdater
) where
import Control.Concurrent
import Control.Applicative
import Updater.Internal hiding (newSignal, readSignal)
import qualified Updater.Internal as Internal
import System.IO.Unsafe
newSignal :: a -> Updater (Signal a)
newSignal = liftSTM . Internal.newSignal
stop :: Updater a
stop = empty
putLine :: String -> Updater ()
putLine = onCommit . putStrLn
local :: Updater a -> Updater ()
local computation = return () <|> (computation >> stop)
readSignal :: Signal a -> Updater a
readSignal = liftSTM . Internal.readSignal
modifySignal :: Signal a -> (a -> a) -> Updater ()
modifySignal s f = readSignal s >>= writeSignal s . f
runGlobalUpdater :: Updater a -> IO ()
runGlobalUpdater u = runUpdater $ writeSignal globalUpdater (u >> return ())
globalUpdater :: Signal (Updater ())
globalUpdater = unsafePerformIO $ do
s <- newSignalIO $ return ()
forkIO $ runUpdater $ do
currentUpdater <-getBehavior s
currentUpdater
stop
return s