eventful-core-0.1.1: Core module for eventful

Safe HaskellNone
LanguageHaskell2010

Eventful.TH.Projection

Synopsis

Documentation

mkProjection :: Name -> Name -> [Name] -> Q [Dec] Source #

Creates a Projection for a given type and a list of events. The user of this function also needs to provide event handlers for each event. For example:

   data EventA = EventA
   data EventB = EventB

   data MyState = MyState Int

   myStateDefault :: MyState
   myStateDefault = MyState 0

   mkProjection ''MyState 'myStateDefault [''EventA, ''EventB]

   handleEventA :: MyState -> EventA -> MyState
   handleEventA (MyState x) EventA = MyState (x + 1)

   handleEventB :: MyState -> EventB -> MyState
   handleEventB (MyState x) EventB = MyState (x - 1)

This will produce the following:

   data MyStateEvent = MyStateEventA !EventA | MyStateEventB !EventB

   handleMyStateEvent :: MyState -> MyStateEvent -> MyState
   handleMyStateEvent state (MyStateEventA event) = handleEventA state event
   handleMyStateEvent state (MyStateEventB event) = handleEventB state event

   type MyStateProjection = Projection MyState MyStateEvent

   myStateProjection :: MyStateProjection
   myStateProjection = Projection myStateDefault handleMyStateEvent