1 {-# LANGUAGE ScopedTypeVariables, FlexibleContexts #-}
3 module RMCA.Auxiliary.RV where
6 import Data.ReactiveValue
9 newCBMVarRW :: forall a. a -> IO (ReactiveFieldReadWrite IO a)
13 getter = readCBMVar mvar
15 setter = writeCBMVar mvar
16 notifier :: IO () -> IO ()
17 notifier = installCallbackCBMVar mvar
18 return $ ReactiveFieldReadWrite setter getter notifier
20 emptyRW :: (Monoid b, ReactiveValueReadWrite a b m) => a -> m b
22 val <- reactiveValueRead rv
23 reactiveValueWrite rv mempty
26 emptyW :: (Monoid b, ReactiveValueWrite a b m) => a -> m ()
27 emptyW rv = reactiveValueWrite rv mempty
29 (^:>) :: (ReactiveValueRead a b m, ReactiveValueReadWrite c d m) =>
31 notif ^:> rv = reactiveValueOnCanRead notif resync
32 where resync = reactiveValueRead rv >>= reactiveValueWrite rv
34 -- Update when the value is an Event. It would be nice to have that
35 -- even for Maybe as well.
36 (>:>) :: (ReactiveValueRead a (Event b) m, ReactiveValueWrite c b m) =>
38 eventRV >:> rv = reactiveValueOnCanRead eventRV syncOnEvent
39 where syncOnEvent = reactiveValueRead eventRV >>=
40 (\erv -> if isNoEvent erv then return ()
41 else reactiveValueWrite rv $ fromEvent erv)