eve-0.1.8: An extensible event framework

Safe HaskellNone




type States = Map TypeRep StateWrapper Source #

A map of state types to their current value.

class HasStates s where Source #

Represents a state which can itself store more states. states is a lens which points to a given state's States map.

Minimal complete definition



states :: Lens' s States Source #

class (Typeable s, HasStates s) => HasEvents s Source #

A typeclass to ensure people don't dispatch events to states which shouldn't accept them.

To allow dispatching events in an action over your state simply define the empty instance:

instance HasEvents MyState where
-- Don't need anything here.

stateLens :: forall a e. (Typeable a, Default a, HasStates e) => Lens' e a Source #

A polymorphic lens which accesses stored states. It returns the default value (def) if a state has not yet been set.

makeStateLens :: (HasStates s, Typeable myState, Default myState) => Lens' myState a -> Lens' s a Source #

A utility which creates a state-nested version of a lens. If you pass this function a lens from your state to one of its fields, it will return a lens which can be used within an App or Action.

The resulting lens will be of type: newLens :: HasStates s => Lens' s MyState Or if you prefer, you may wish to specify the state it operates over more specifically to prevent using the lens where it was not originally planned. For instance: newLens :: Lens' AppState MyState

data SimpleState = SimpleState
  { _myString :: String
makeLenses ''SimpleState

instance Default SimpleState where
  def = SimpleState "default"

myStringStateLens :: HasStates s => Lens' s String
myStringStateLens = makeStateLens myString

myAction :: App ()
myAction = do
  myStringStateLens .= "Hi!"
  str <- use myStringStateLens
  liftIO $ print str
-- "Hi!"

For more complex Prisms or Traversals you can write your own using stateLens