1 {-# LANGUAGE ScopedTypeVariables, FlexibleContexts #-}
3 module RMCA.Auxiliary.RV where
6 import Data.ReactiveValue
9 import RMCA.Auxiliary.Curry
11 newCBMVarRW :: forall a. a -> IO (ReactiveFieldReadWrite IO a)
15 getter = readCBMVar mvar
17 setter = writeCBMVar mvar
18 notifier :: IO () -> IO ()
19 notifier = installCallbackCBMVar mvar
20 return $ ReactiveFieldReadWrite setter getter notifier
22 emptyRW :: (Monoid b, ReactiveValueReadWrite a b m) => a -> m b
24 val <- reactiveValueRead rv
25 reactiveValueWrite rv mempty
28 emptyW :: (Monoid b, ReactiveValueWrite a b m) => a -> m ()
29 emptyW rv = reactiveValueWrite rv mempty
31 onTick :: (ReactiveValueRead a b m, ReactiveValueRead c d m) =>
32 a -> c -> ReactiveFieldRead m d
33 onTick notif rv = ReactiveFieldRead getter notifier
34 where getter = reactiveValueRead rv
36 reactiveValueOnCanRead notif cb
37 reactiveValueOnCanRead rv cb
39 addHandlerR :: (ReactiveValueRead a b m) =>
42 -> ReactiveFieldRead m b
43 addHandlerR x h = ReactiveFieldRead (reactiveValueRead x)
44 (\p -> reactiveValueOnCanRead x p >> h p)
47 reactiveValueOnCanRead notif (reactiveValueOnCanRead rv (return ()))
49 -- Update when the value is an Event. It would be nice to have that
50 -- even for Maybe as well.
51 (>:>) :: (ReactiveValueRead a (Event b) IO, ReactiveValueWrite c b IO) =>
53 eventRV >:> rv = reactiveValueOnCanRead eventRV syncOnEvent
54 where syncOnEvent = do
55 erv <- reactiveValueRead eventRV
56 when (isEvent erv) $ reactiveValueWrite rv $ fromEvent erv
59 , ReactiveValueRead a b m
60 , ReactiveValueRead c d m
61 , ReactiveValueRead e f m) =>
66 -> ReactiveFieldRead m i
67 liftR3 f a b c = ReactiveFieldRead getter notifier
69 x1 <- reactiveValueRead a
70 x2 <- reactiveValueRead b
71 x3 <- reactiveValueRead c
72 return $ f (x1, x2, x3)
73 notifier p = reactiveValueOnCanRead a p >>
74 reactiveValueOnCanRead b p >>
75 reactiveValueOnCanRead c p
79 , ReactiveValueWrite a b m
80 , ReactiveValueWrite c d m
81 , ReactiveValueWrite e f m) =>
86 -> ReactiveFieldWrite m i
87 liftW3 f a b c = ReactiveFieldWrite setter
90 reactiveValueWrite a x1
91 reactiveValueWrite b x2
92 reactiveValueWrite c x3
95 , ReactiveValueReadWrite a b m
96 , ReactiveValueReadWrite c d m
97 , ReactiveValueReadWrite e f m) =>
98 BijectiveFunc i (b,d,f)
102 -> ReactiveFieldReadWrite m i
104 ReactiveFieldReadWrite setter getter notifier
105 where ReactiveFieldRead getter notifier = liftR3 (curry3 f2) a b c
106 ReactiveFieldWrite setter = liftW3 f1 a b c
107 (f1, f2) = (direct bij, inverse bij)
110 , ReactiveValueRead a b m
111 , ReactiveValueRead c d m
112 , ReactiveValueRead e f m
113 , ReactiveValueRead g h m) =>
119 -> ReactiveFieldRead m i
120 liftR4 f a b c d = ReactiveFieldRead getter notifier
122 x1 <- reactiveValueRead a
123 x2 <- reactiveValueRead b
124 x3 <- reactiveValueRead c
125 x4 <- reactiveValueRead d
126 return $ f (x1, x2, x3, x4)
128 reactiveValueOnCanRead a p
129 reactiveValueOnCanRead b p
130 reactiveValueOnCanRead c p
131 reactiveValueOnCanRead d p
134 , ReactiveValueWrite a b m
135 , ReactiveValueWrite c d m
136 , ReactiveValueWrite e f m
137 , ReactiveValueWrite g h m) =>
143 -> ReactiveFieldWrite m i
144 liftW4 f a b c d = ReactiveFieldWrite setter
146 let (x1,x2,x3,x4) = f x
147 reactiveValueWrite a x1
148 reactiveValueWrite b x2
149 reactiveValueWrite c x3
150 reactiveValueWrite d x4
153 , ReactiveValueReadWrite a b m
154 , ReactiveValueReadWrite c d m
155 , ReactiveValueReadWrite e f m
156 , ReactiveValueReadWrite g h m) =>
157 BijectiveFunc i (b,d,f,h)
162 -> ReactiveFieldReadWrite m i
163 liftRW4 bij a b c d =
164 ReactiveFieldReadWrite setter getter notifier
165 where ReactiveFieldRead getter notifier = liftR4 f2 a b c d
166 ReactiveFieldWrite setter = liftW4 f1 a b c d
167 (f1, f2) = (direct bij, inverse bij)