lambdabot-core-5.3.0.1: Lambdabot core functionality

Safe HaskellNone
LanguageHaskell98

Lambdabot.State

Contents

Description

Support for the LB (LambdaBot) monad

Synopsis

Functions to access the module's state

class MonadLB m => MonadLBState m where Source #

Associated Types

type LBState m 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
MonadLBState m => MonadLBState (Cmd m) Source # 
Instance details

Defined in Lambdabot.State

Associated Types

type LBState (Cmd m) :: Type Source #

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) :: Type Source #

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