1 {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
3 module RMCA.ReactiveValueAtomicUpdate where
7 import Data.ReactiveValue
9 reactiveValueNonAtomicUpdate :: (ReactiveValueReadWrite a b m) =>
11 reactiveValueNonAtomicUpdate rv f = do
12 val <- reactiveValueRead rv
13 reactiveValueWrite rv $ f val
16 class (ReactiveValueReadWrite a b m) => ReactiveValueAtomicUpdate a b m where
17 reactiveValueUpdate :: a -> (b -> b) -> m b
19 reactiveValueUpdate_ :: (ReactiveValueAtomicUpdate a b m) =>
21 reactiveValueUpdate_ rv f = void $ reactiveValueUpdate rv f
23 reactiveValueAppend :: (Monoid b, ReactiveValueAtomicUpdate a b m) =>
25 reactiveValueAppend rv val = reactiveValueUpdate_ rv (`mappend` val)
27 reactiveValueEmpty :: (Monoid b, ReactiveValueAtomicUpdate a b m) =>
29 reactiveValueEmpty rv = reactiveValueUpdate rv (const mempty)
31 instance ReactiveValueRead (CBRef a) a IO where
32 reactiveValueRead = readCBRef
33 reactiveValueOnCanRead = installCallbackCBRef
35 instance ReactiveValueWrite (CBRef a) a IO where
36 reactiveValueWrite = writeCBRef
38 instance ReactiveValueReadWrite (CBRef a) a IO where
40 instance ReactiveValueAtomicUpdate (CBRef a) a IO where
41 reactiveValueUpdate rv f = atomicModifyCBRef rv (\x -> (f x, x))