1 {-# LANGUAGE ScopedTypeVariables, FlexibleContexts #-}
3 module RMCA.Auxiliary.RV where
6 import Data.ReactiveValue
10 newCBMVarRW :: forall a. a -> IO (ReactiveFieldReadWrite IO a)
14 getter = readCBMVar mvar
16 setter = writeCBMVar mvar
17 notifier :: IO () -> IO ()
18 notifier = installCallbackCBMVar mvar
19 return $ ReactiveFieldReadWrite setter getter notifier
21 emptyRW :: (Monoid b, ReactiveValueReadWrite a b m) => a -> m b
23 val <- reactiveValueRead rv
24 reactiveValueWrite rv mempty
27 emptyW :: (Monoid b, ReactiveValueWrite a b m) => a -> m ()
28 emptyW rv = reactiveValueWrite rv mempty
30 (^:>) :: (ReactiveValueRead a b m, ReactiveValueReadWrite c d m) =>
32 notif ^:> rv = reactiveValueOnCanRead notif resync
33 where resync = reactiveValueRead rv >>= reactiveValueWrite rv
35 -- Update when the value is an Event. It would be nice to have that
36 -- even for Maybe as well.
37 (>:>) :: (ReactiveValueRead a (Event b) m, ReactiveValueWrite c b m) =>
39 eventRV >:> rv = reactiveValueOnCanRead eventRV syncOnEvent
40 where syncOnEvent = do
41 erv <- reactiveValueRead eventRV
42 when (isEvent erv) $ reactiveValueWrite rv $ fromEvent erv
45 , ReactiveValueRead a b m
46 , ReactiveValueRead c d m
47 , ReactiveValueRead e f m) =>
52 -> ReactiveFieldRead m i
53 liftR3 f a b c = ReactiveFieldRead getter notifier
55 x1 <- reactiveValueRead a
56 x2 <- reactiveValueRead b
57 x3 <- reactiveValueRead c
58 return $ f (x1, x2, x3)
59 notifier p = reactiveValueOnCanRead a p >>
60 reactiveValueOnCanRead b p >>
61 reactiveValueOnCanRead c p
64 , ReactiveValueWrite a b m
65 , ReactiveValueWrite c d m
66 , ReactiveValueWrite e f m) =>
71 -> ReactiveFieldWrite m i
72 liftW3 f a b c = ReactiveFieldWrite setter
75 reactiveValueWrite a x1
76 reactiveValueWrite b x2
77 reactiveValueWrite c x3
80 , ReactiveValueRead a b m
81 , ReactiveValueRead c d m
82 , ReactiveValueRead e f m
83 , ReactiveValueRead g h m) =>
89 -> ReactiveFieldRead m i
90 liftR4 f a b c d = ReactiveFieldRead getter notifier
92 x1 <- reactiveValueRead a
93 x2 <- reactiveValueRead b
94 x3 <- reactiveValueRead c
95 x4 <- reactiveValueRead d
96 return $ f (x1, x2, x3, x4)
97 notifier p = reactiveValueOnCanRead a p >>
98 reactiveValueOnCanRead b p >>
99 reactiveValueOnCanRead c p >>
100 reactiveValueOnCanRead d p
103 , ReactiveValueWrite a b m
104 , ReactiveValueWrite c d m
105 , ReactiveValueWrite e f m
106 , ReactiveValueWrite g h m) =>
112 -> ReactiveFieldWrite m i
113 liftW4 f a b c d = ReactiveFieldWrite setter
115 let (x1,x2,x3,x4) = f x
116 reactiveValueWrite a x1
117 reactiveValueWrite b x2
118 reactiveValueWrite c x3
119 reactiveValueWrite d x4
122 , ReactiveValueReadWrite a b m
123 , ReactiveValueReadWrite c d m
124 , ReactiveValueReadWrite e f m
125 , ReactiveValueReadWrite g h m) =>
126 BijectiveFunc i (b,d,f,h)
131 -> ReactiveFieldReadWrite m i
132 liftRW4 bij a b c d =
133 ReactiveFieldReadWrite setter getter notifier
134 where ReactiveFieldRead getter notifier = liftR4 f2 a b c d
135 ReactiveFieldWrite setter = liftW4 f1 a b c d
136 (f1, f2) = (direct bij, inverse bij)