1 {-# LANGUAGE ScopedTypeVariables, FlexibleContexts #-}
3 module RMCA.Auxiliary.RV where
6 import Data.ReactiveValue
9 import RMCA.Auxiliary.Curry
11 leftSyncWith :: (ReactiveValueRead a b m, ReactiveValueWrite c d m) =>
12 (b -> d) -> a -> c -> m ()
13 leftSyncWith f a c = reactiveValueOnCanRead a
14 (reactiveValueRead a >>= reactiveValueWrite c . f)
16 (=:$:>) :: (ReactiveValueRead a b m, ReactiveValueWrite c d m) =>
17 (b -> d) -> a -> c -> m ()
18 (=:$:>) = leftSyncWith
20 newCBMVarRW :: forall a. a -> IO (ReactiveFieldReadWrite IO a)
24 getter = readCBMVar mvar
26 setter = writeCBMVar mvar
27 notifier :: IO () -> IO ()
28 notifier = installCallbackCBMVar mvar
29 return $ ReactiveFieldReadWrite setter getter notifier
31 emptyRW :: (Monoid b, ReactiveValueReadWrite a b m) => a -> m b
33 val <- reactiveValueRead rv
34 reactiveValueWrite rv mempty
37 emptyW :: (Monoid b, ReactiveValueWrite a b m) => a -> m ()
38 emptyW rv = reactiveValueWrite rv mempty
40 onTick :: (ReactiveValueRead a b m, ReactiveValueRead c d m) =>
41 a -> c -> ReactiveFieldRead m d
42 onTick notif rv = ReactiveFieldRead getter notifier
43 where getter = reactiveValueRead rv
45 reactiveValueOnCanRead notif cb
46 reactiveValueOnCanRead rv cb
48 addHandlerR :: (ReactiveValueRead a b m) =>
51 -> ReactiveFieldRead m b
52 addHandlerR x h = ReactiveFieldRead (reactiveValueRead x)
53 (\p -> reactiveValueOnCanRead x p >> h p)
56 reactiveValueOnCanRead notif (reactiveValueOnCanRead rv (return ()))
58 -- Update when the value is an Event. It would be nice to have that
59 -- even for Maybe as well.
60 (>:>) :: (ReactiveValueRead a (Event b) IO, ReactiveValueWrite c b IO) =>
62 eventRV >:> rv = reactiveValueOnCanRead eventRV syncOnEvent
63 where syncOnEvent = do
64 erv <- reactiveValueRead eventRV
65 when (isEvent erv) $ reactiveValueWrite rv $ fromEvent erv
68 , ReactiveValueRead a b m
69 , ReactiveValueRead c d m
70 , ReactiveValueRead e f m) =>
75 -> ReactiveFieldRead m i
76 liftR3 f a b c = ReactiveFieldRead getter notifier
78 x1 <- reactiveValueRead a
79 x2 <- reactiveValueRead b
80 x3 <- reactiveValueRead c
81 return $ f (x1, x2, x3)
82 notifier p = reactiveValueOnCanRead a p >>
83 reactiveValueOnCanRead b p >>
84 reactiveValueOnCanRead c p
88 , ReactiveValueWrite a b m
89 , ReactiveValueWrite c d m
90 , ReactiveValueWrite e f m) =>
95 -> ReactiveFieldWrite m i
96 liftW3 f a b c = ReactiveFieldWrite setter
99 reactiveValueWrite a x1
100 reactiveValueWrite b x2
101 reactiveValueWrite c x3
103 liftRW3 :: ( ReactiveValueReadWrite a b m
104 , ReactiveValueReadWrite c d m
105 , ReactiveValueReadWrite e f m) =>
106 BijectiveFunc i (b,d,f)
110 -> ReactiveFieldReadWrite m i
112 ReactiveFieldReadWrite setter getter notifier
113 where ReactiveFieldRead getter notifier = liftR3 (curry3 f2) a b c
114 ReactiveFieldWrite setter = liftW3 f1 a b c
115 (f1, f2) = (direct bij, inverse bij)
117 liftR4 :: ( ReactiveValueRead a b m
118 , ReactiveValueRead c d m
119 , ReactiveValueRead e f m
120 , ReactiveValueRead g h m) =>
126 -> ReactiveFieldRead m i
127 liftR4 f a b c d = ReactiveFieldRead getter notifier
129 x1 <- reactiveValueRead a
130 x2 <- reactiveValueRead b
131 x3 <- reactiveValueRead c
132 x4 <- reactiveValueRead d
133 return $ f (x1, x2, x3, x4)
135 reactiveValueOnCanRead a p
136 reactiveValueOnCanRead b p
137 reactiveValueOnCanRead c p
138 reactiveValueOnCanRead d p
141 , ReactiveValueWrite a b m
142 , ReactiveValueWrite c d m
143 , ReactiveValueWrite e f m
144 , ReactiveValueWrite g h m) =>
150 -> ReactiveFieldWrite m i
151 liftW4 f a b c d = ReactiveFieldWrite setter
153 let (x1,x2,x3,x4) = f x
154 reactiveValueWrite a x1
155 reactiveValueWrite b x2
156 reactiveValueWrite c x3
157 reactiveValueWrite d x4
159 liftRW4 :: ( ReactiveValueReadWrite a b m
160 , ReactiveValueReadWrite c d m
161 , ReactiveValueReadWrite e f m
162 , ReactiveValueReadWrite g h m) =>
163 BijectiveFunc i (b,d,f,h)
168 -> ReactiveFieldReadWrite m i
169 liftRW4 bij a b c d =
170 ReactiveFieldReadWrite setter getter notifier
171 where ReactiveFieldRead getter notifier = liftR4 f2 a b c d
172 ReactiveFieldWrite setter = liftW4 f1 a b c d
173 (f1, f2) = (direct bij, inverse bij)