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
104 , ReactiveValueReadWrite a b m
105 , ReactiveValueReadWrite c d m
106 , ReactiveValueReadWrite e f m) =>
107 BijectiveFunc i (b,d,f)
111 -> ReactiveFieldReadWrite m i
113 ReactiveFieldReadWrite setter getter notifier
114 where ReactiveFieldRead getter notifier = liftR3 (curry3 f2) a b c
115 ReactiveFieldWrite setter = liftW3 f1 a b c
116 (f1, f2) = (direct bij, inverse bij)
119 , ReactiveValueRead a b m
120 , ReactiveValueRead c d m
121 , ReactiveValueRead e f m
122 , ReactiveValueRead g h m) =>
128 -> ReactiveFieldRead m i
129 liftR4 f a b c d = ReactiveFieldRead getter notifier
131 x1 <- reactiveValueRead a
132 x2 <- reactiveValueRead b
133 x3 <- reactiveValueRead c
134 x4 <- reactiveValueRead d
135 return $ f (x1, x2, x3, x4)
137 reactiveValueOnCanRead a p
138 reactiveValueOnCanRead b p
139 reactiveValueOnCanRead c p
140 reactiveValueOnCanRead d p
143 , ReactiveValueWrite a b m
144 , ReactiveValueWrite c d m
145 , ReactiveValueWrite e f m
146 , ReactiveValueWrite g h m) =>
152 -> ReactiveFieldWrite m i
153 liftW4 f a b c d = ReactiveFieldWrite setter
155 let (x1,x2,x3,x4) = f x
156 reactiveValueWrite a x1
157 reactiveValueWrite b x2
158 reactiveValueWrite c x3
159 reactiveValueWrite d x4
162 , ReactiveValueReadWrite a b m
163 , ReactiveValueReadWrite c d m
164 , ReactiveValueReadWrite e f m
165 , ReactiveValueReadWrite g h m) =>
166 BijectiveFunc i (b,d,f,h)
171 -> ReactiveFieldReadWrite m i
172 liftRW4 bij a b c d =
173 ReactiveFieldReadWrite setter getter notifier
174 where ReactiveFieldRead getter notifier = liftR4 f2 a b c d
175 ReactiveFieldWrite setter = liftW4 f1 a b c d
176 (f1, f2) = (direct bij, inverse bij)