1 {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module RMCA.ReactiveValueAtomicUpdate where
8 import Data.ReactiveValue
10 reactiveValueNonAtomicUpdate :: (ReactiveValueReadWrite a b m) =>
12 reactiveValueNonAtomicUpdate rv f = do
13 val <- reactiveValueRead rv
14 reactiveValueWrite rv $ f val
17 class (ReactiveValueReadWrite a b m) => ReactiveValueAtomicUpdate a b m where
18 reactiveValueUpdate :: a -> (b -> b) -> m b
20 reactiveValueUpdate_ :: (ReactiveValueAtomicUpdate a b m) =>
22 reactiveValueUpdate_ rv f = reactiveValueUpdate rv f >> return ()
24 reactiveValueAppend :: (Monoid b, ReactiveValueAtomicUpdate a b m) =>
26 reactiveValueAppend rv val = reactiveValueUpdate_ rv (`mappend` val)
28 reactiveValueEmpty :: (Monoid b, ReactiveValueAtomicUpdate a b m) =>
30 reactiveValueEmpty rv = reactiveValueUpdate rv (const mempty)
32 instance ReactiveValueRead (CBRef a) a IO where
33 reactiveValueRead = readCBRef
34 reactiveValueOnCanRead = installCallbackCBRef
36 instance ReactiveValueWrite (CBRef a) a IO where
37 reactiveValueWrite = writeCBRef
39 instance ReactiveValueReadWrite (CBRef a) a IO where
41 instance ReactiveValueAtomicUpdate (CBRef a) a IO where
42 reactiveValueUpdate rv f = atomicModifyCBRef rv (\x -> (f x, x))