lambdabot-core-5.3.1.2: Lambdabot core functionality

Safe Haskell | None
Language | Haskell98

Lambdabot.State

Contents

- Functions to access the module's state
- Utility functions for modules that need state for each target.
- Handling global state

Description

Support for the LB (LambdaBot) monad

Synopsis

- class MonadLB m => MonadLBState (m :: Type -> Type) where
  - type LBState (m :: Type -> Type)
  - withMS :: (LBState m -> (LBState m -> m ()) -> m a) -> m a
- readMS :: MonadLBState m => m (LBState m)
- writeMS :: MonadLBState m => LBState m -> m ()
- modifyMS :: MonadLBState m => (LBState m -> LBState m) -> m ()
- data GlobalPrivate g p
- mkGlobalPrivate :: Int -> g -> GlobalPrivate g p
- withPS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => Nick -> (Maybe p -> (Maybe p -> LB ()) -> LB a) -> m a
- readPS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => Nick -> m (Maybe p)
- writePS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => Nick -> Maybe p -> m ()
- withGS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => (g -> (g -> m ()) -> m ()) -> m ()
- readGS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => m g
- writeGS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => g -> m ()
- readGlobalState :: Module st -> String -> LB (Maybe st)
- writeGlobalState :: ModuleT st LB ()

## Functions to access the module's state

class MonadLB m => MonadLBState (m :: Type -> Type) where Source #

Associated Types

type LBState (m :: Type -> Type) Source #

Methods

withMS :: (LBState m -> (LBState m -> m ()) -> m a) -> m a Source #

Update the module's private state. This is the preferred way of changing the state. The state will be locked until the body returns. The function is exception-safe, i.e. even if an error occurs or the thread is killed (e.g. because it deadlocked and therefore exceeded its time limit), the state from the last write operation will be restored. If the writer escapes, calling it will have no observable effect. `withMS` is not composable, in the sense that a readMS from within the body will cause a dead-lock. However, all other possibilies to access the state that came to my mind had even more serious deficiencies such as being prone to race conditions or semantic obscurities.

#### Instances

Instances details

MonadLBState m => MonadLBState (Cmd m) Source # |

Instance details

Defined in Lambdabot.State

Associated Types

type LBState (Cmd m) |
Instance details Defined in Lambdabot.State type LBState (Cmd m) = LBState m

Methods

withMS :: (LBState (Cmd m) -> (LBState (Cmd m) -> Cmd m ()) -> Cmd m a) -> Cmd m a Source #

MonadLB m => MonadLBState (ModuleT st m) Source # |

Instance details

Defined in Lambdabot.State

Associated Types

type LBState (ModuleT st m) |
Instance details Defined in Lambdabot.State type LBState (ModuleT st m) = st

Methods

withMS :: (LBState (ModuleT st m) -> (LBState (ModuleT st m) -> ModuleT st m ()) -> ModuleT st m a) -> ModuleT st m a Source #

readMS :: MonadLBState m => m (LBState m) Source #

Read the module's private state.

writeMS :: MonadLBState m => LBState m -> m () Source #

Write the module's private state. Try to use withMS instead.

modifyMS :: MonadLBState m => (LBState m -> LBState m) -> m () Source #

Modify the module's private state.

## Utility functions for modules that need state for each target.

data GlobalPrivate g p Source #

This datatype allows modules to conviently maintain both global (i.e. for all clients they're interacting with) and private state. It is implemented on top of readMS/withMS.

This simple implementation is linear in the number of private states used.

mkGlobalPrivate :: Int -> g -> GlobalPrivate g p Source #

Creates a `GlobalPrivate` given the value of the global state. No private state for clients will be created.

withPS Source #

Arguments

:: (MonadLBState m, LBState m ~ GlobalPrivate g p) |
=> Nick | The target
-> (Maybe p -> (Maybe p -> LB ()) -> LB a) | `Just x` writes x in the user's private state, `Nothing` removes it.
-> m a |

Writes private state. For now, it locks everything.

readPS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => Nick -> m (Maybe p) Source #

Reads private state.

writePS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => Nick -> Maybe p -> m () Source #

withGS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => (g -> (g -> m ()) -> m ()) -> m () Source #

Writes global state. Locks everything

readGS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => m g Source #

Reads global state.

writeGS :: (MonadLBState m, LBState m ~ GlobalPrivate g p) => g -> m () Source #

## Handling global state

readGlobalState :: Module st -> String -> LB (Maybe st) Source #

Read it in

writeGlobalState :: ModuleT st LB () Source #

Peristence: write the global state out