module Data.Acid.Memory.Pure
( IsAcidic(..)
, AcidState
, Event(..)
, EventResult
, EventState
, UpdateEvent
, QueryEvent
, Update
, Query
, openAcidState
, update
, update_
, query
, liftQuery
, runUpdate
, runQuery
) where
import Data.Acid.Core
import Data.Acid.Common
import Control.Monad.State
import Control.Monad.Reader
data AcidState st
= AcidState { localMethods :: MethodMap st
, localState :: st
}
update :: UpdateEvent event => AcidState (EventState event) -> event -> ( AcidState (EventState event)
, EventResult event)
update acidState event
= case runState hotMethod (localState acidState) of
!(result, !newState) -> ( acidState { localState = newState }
, result )
where hotMethod = lookupHotMethod (localMethods acidState) event
update_ :: UpdateEvent event => AcidState (EventState event) -> event -> AcidState (EventState event)
update_ acidState event
= fst (update acidState event)
query :: QueryEvent event => AcidState (EventState event) -> event -> EventResult event
query acidState event
= case runState hotMethod (localState acidState) of
!(result, !_st) -> result
where hotMethod = lookupHotMethod (localMethods acidState) event
openAcidState :: IsAcidic st
=> st
-> AcidState st
openAcidState initialState
= AcidState { localMethods = mkMethodMap (eventsToMethods acidEvents)
, localState = initialState }
runUpdate :: Update s r -> s -> (r, s)
runUpdate update = runState $ unUpdate update
runQuery :: Query s r -> s -> r
runQuery query = runReader $ unQuery query