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 (^:>) :: (ReactiveValueRead a b m, ReactiveValueRead c d m) =>
34 notif ^:> rv = reactiveValueOnCanRead notif resync
35 where resync = reactiveValueRead rv >>= reactiveValueWrite rv
38 reactiveValueOnCanRead notif (reactiveValueOnCanRead rv (return ()))
40 -- Update when the value is an Event. It would be nice to have that
41 -- even for Maybe as well.
42 (>:>) :: (ReactiveValueRead a (Event b) m, ReactiveValueWrite c b m) =>
44 eventRV >:> rv = reactiveValueOnCanRead eventRV syncOnEvent
45 where syncOnEvent = do
46 erv <- reactiveValueRead eventRV
47 when (isEvent erv) $ reactiveValueWrite rv $ fromEvent erv
50 , ReactiveValueRead a b m
51 , ReactiveValueRead c d m
52 , ReactiveValueRead e f m) =>
57 -> ReactiveFieldRead m i
58 liftR3 f a b c = ReactiveFieldRead getter notifier
60 x1 <- reactiveValueRead a
61 x2 <- reactiveValueRead b
62 x3 <- reactiveValueRead c
63 return $ f (x1, x2, x3)
64 notifier p = reactiveValueOnCanRead a p >>
65 reactiveValueOnCanRead b p >>
66 reactiveValueOnCanRead c p
70 , ReactiveValueWrite a b m
71 , ReactiveValueWrite c d m
72 , ReactiveValueWrite e f m) =>
77 -> ReactiveFieldWrite m i
78 liftW3 f a b c = ReactiveFieldWrite setter
81 reactiveValueWrite a x1
82 reactiveValueWrite b x2
83 reactiveValueWrite c x3
86 , ReactiveValueReadWrite a b m
87 , ReactiveValueReadWrite c d m
88 , ReactiveValueReadWrite e f m) =>
89 BijectiveFunc i (b,d,f)
93 -> ReactiveFieldReadWrite m i
95 ReactiveFieldReadWrite setter getter notifier
96 where ReactiveFieldRead getter notifier = liftR3 (curry3 f2) a b c
97 ReactiveFieldWrite setter = liftW3 f1 a b c
98 (f1, f2) = (direct bij, inverse bij)
101 , ReactiveValueRead a b m
102 , ReactiveValueRead c d m
103 , ReactiveValueRead e f m
104 , ReactiveValueRead g h m) =>
110 -> ReactiveFieldRead m i
111 liftR4 f a b c d = ReactiveFieldRead getter notifier
113 x1 <- reactiveValueRead a
114 x2 <- reactiveValueRead b
115 x3 <- reactiveValueRead c
116 x4 <- reactiveValueRead d
117 return $ f (x1, x2, x3, x4)
118 notifier p = reactiveValueOnCanRead a p >>
119 reactiveValueOnCanRead b p >>
120 reactiveValueOnCanRead c p >>
121 reactiveValueOnCanRead d p
124 , ReactiveValueWrite a b m
125 , ReactiveValueWrite c d m
126 , ReactiveValueWrite e f m
127 , ReactiveValueWrite g h m) =>
133 -> ReactiveFieldWrite m i
134 liftW4 f a b c d = ReactiveFieldWrite setter
136 let (x1,x2,x3,x4) = f x
137 reactiveValueWrite a x1
138 reactiveValueWrite b x2
139 reactiveValueWrite c x3
140 reactiveValueWrite d x4
143 , ReactiveValueReadWrite a b m
144 , ReactiveValueReadWrite c d m
145 , ReactiveValueReadWrite e f m
146 , ReactiveValueReadWrite g h m) =>
147 BijectiveFunc i (b,d,f,h)
152 -> ReactiveFieldReadWrite m i
153 liftRW4 bij a b c d =
154 ReactiveFieldReadWrite setter getter notifier
155 where ReactiveFieldRead getter notifier = liftR4 f2 a b c d
156 ReactiveFieldWrite setter = liftW4 f1 a b c d
157 (f1, f2) = (direct bij, inverse bij)