Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module contains a type class that describes exchangable operations on messages boxes.
Synopsis
- class (IsMessageBox (MessageBox argument), IsInput (Input (MessageBox argument))) => IsMessageBoxArg argument where
- type MessageBox argument :: Type -> Type
- getConfiguredMessageLimit :: argument -> Maybe Int
- newMessageBox :: MonadUnliftIO m => argument -> m (MessageBox argument a)
- class IsInput (Input box) => IsMessageBox box where
- type Input box :: Type -> Type
- receive :: MonadUnliftIO m => box a -> m (Maybe a)
- tryReceive :: MonadUnliftIO m => box a -> m (Future a)
- receiveAfter :: MonadUnliftIO m => box a -> Int -> m (Maybe a)
- newInput :: MonadUnliftIO m => box a -> m (Input box a)
- class IsInput input where
- handleMessage :: (MonadUnliftIO m, IsMessageBox box) => box message -> (message -> m b) -> m (Maybe b)
Documentation
class (IsMessageBox (MessageBox argument), IsInput (Input (MessageBox argument))) => IsMessageBoxArg argument where #
Types that configure and allow the creation of a MessageBox
.
Create IsMessageBox
instances from a parameter.
Types that determine MessageBox
values.
For a limited message box this might be the limit of the message queue.
type MessageBox argument :: Type -> Type #
The message box that can be created from the message box argument
getConfiguredMessageLimit :: argument -> Maybe Int #
Return a message limit.
NOTE: This method was added for unit tests. Although the method is totally valid, it might not be super useful in production code. Also note that the naming follows the rule: Reserve short names for entities that are used often.
newMessageBox :: MonadUnliftIO m => argument -> m (MessageBox argument a) #
Create a new msgBox
according to the argument
.
This is required to receive a message.
NOTE: Only one process may receive on an msgBox.
Instances
class IsInput (Input box) => IsMessageBox box where #
A type class for msgBox types. A common interface for receiving messages.
receive :: MonadUnliftIO m => box a -> m (Maybe a) #
Receive a message. Take whatever time it takes.
Return Just
the value or Nothing
when an error
occurred.
NOTE: Nothing may sporadically be returned, especially when there is a lot of load, so please make sure to build your application in such a way, that it anticipates failure.
tryReceive :: MonadUnliftIO m => box a -> m (Future a) #
Return a Future
that can be used to wait for the
arrival of the next message.
NOTE: Each future value represents the next slot in the queue
so one future corresponds to exactly that message (should it arrive)
and if that future value is dropped, that message will be lost!
:: MonadUnliftIO m | |
=> box a | Message box |
-> Int | Time in micro seconds to wait until the action is invoked. |
-> m (Maybe a) |
Wait for an incoming message or return Nothing.
The default implementation uses tryReceive
to get a
Future
on which awaitFuture
inside a timeout
is called.
Instances might override this with more performant implementations especially non-blocking Unagi channel based implementation.
NOTE: Nothing may sporadically be returned, especially when there is a lot of load, so please make sure to build your application in such a way, that it anticipates failure.
newInput :: MonadUnliftIO m => box a -> m (Input box a) #
Create a new input
that enqueus messages,
which are received by the box
Instances
IsMessageBox UnlimitedBox # | A blocking instance that invokes |
Defined in UnliftIO.MessageBox.Unlimited type Input UnlimitedBox :: Type -> Type # receive :: MonadUnliftIO m => UnlimitedBox a -> m (Maybe a) # tryReceive :: MonadUnliftIO m => UnlimitedBox a -> m (Future a) # receiveAfter :: MonadUnliftIO m => UnlimitedBox a -> Int -> m (Maybe a) # newInput :: MonadUnliftIO m => UnlimitedBox a -> m (Input UnlimitedBox a) # | |
IsMessageBox WaitingBox # | |
Defined in UnliftIO.MessageBox.Limited type Input WaitingBox :: Type -> Type # receive :: MonadUnliftIO m => WaitingBox a -> m (Maybe a) # tryReceive :: MonadUnliftIO m => WaitingBox a -> m (Future a) # receiveAfter :: MonadUnliftIO m => WaitingBox a -> Int -> m (Maybe a) # newInput :: MonadUnliftIO m => WaitingBox a -> m (Input WaitingBox a) # | |
IsMessageBox NonBlockingBox # | |
Defined in UnliftIO.MessageBox.Limited type Input NonBlockingBox :: Type -> Type # receive :: MonadUnliftIO m => NonBlockingBox a -> m (Maybe a) # tryReceive :: MonadUnliftIO m => NonBlockingBox a -> m (Future a) # receiveAfter :: MonadUnliftIO m => NonBlockingBox a -> Int -> m (Maybe a) # newInput :: MonadUnliftIO m => NonBlockingBox a -> m (Input NonBlockingBox a) # | |
IsMessageBox BlockingBox # | A blocking instance that invokes |
Defined in UnliftIO.MessageBox.Limited type Input BlockingBox :: Type -> Type # receive :: MonadUnliftIO m => BlockingBox a -> m (Maybe a) # tryReceive :: MonadUnliftIO m => BlockingBox a -> m (Future a) # receiveAfter :: MonadUnliftIO m => BlockingBox a -> Int -> m (Maybe a) # newInput :: MonadUnliftIO m => BlockingBox a -> m (Input BlockingBox a) # | |
IsMessageBox box => IsMessageBox (CatchAllBox box) # | |
Defined in UnliftIO.MessageBox.CatchAll type Input (CatchAllBox box) :: Type -> Type # receive :: MonadUnliftIO m => CatchAllBox box a -> m (Maybe a) # tryReceive :: MonadUnliftIO m => CatchAllBox box a -> m (Future a) # receiveAfter :: MonadUnliftIO m => CatchAllBox box a -> Int -> m (Maybe a) # newInput :: MonadUnliftIO m => CatchAllBox box a -> m (Input (CatchAllBox box) a) # |
A type class for input types. A common interface for delivering messages.
deliver :: MonadUnliftIO m => input a -> a -> m Bool #
Send a message. Take whatever time it takes. Depending on the implementation, this might be a non-blocking operation. Return if the operation was successful.
NOTE: False
may sporadically be returned, especially
when there is a lot of load, so please make sure to
build your application in such a way, that it
anticipates failure.
Instances
IsInput UnlimitedBoxInput # | A blocking instance that invokes |
Defined in UnliftIO.MessageBox.Unlimited deliver :: MonadUnliftIO m => UnlimitedBoxInput a -> a -> m Bool # deliver_ :: MonadUnliftIO m => UnlimitedBoxInput a -> a -> m () # | |
IsInput WaitingInput # | |
Defined in UnliftIO.MessageBox.Limited deliver :: MonadUnliftIO m => WaitingInput a -> a -> m Bool # deliver_ :: MonadUnliftIO m => WaitingInput a -> a -> m () # | |
IsInput NonBlockingInput # | |
Defined in UnliftIO.MessageBox.Limited deliver :: MonadUnliftIO m => NonBlockingInput a -> a -> m Bool # deliver_ :: MonadUnliftIO m => NonBlockingInput a -> a -> m () # | |
IsInput BlockingInput # | A blocking instance that invokes |
Defined in UnliftIO.MessageBox.Limited deliver :: MonadUnliftIO m => BlockingInput a -> a -> m Bool # deliver_ :: MonadUnliftIO m => BlockingInput a -> a -> m () # | |
IsInput i => IsInput (CatchAllInput i) # | |
Defined in UnliftIO.MessageBox.CatchAll deliver :: MonadUnliftIO m => CatchAllInput i a -> a -> m Bool # deliver_ :: MonadUnliftIO m => CatchAllInput i a -> a -> m () # |
handleMessage :: (MonadUnliftIO m, IsMessageBox box) => box message -> (message -> m b) -> m (Maybe b) #
Receive a message and apply a function to it.