top-0.2.4: Top (typed oriented protocol) API

Safe HaskellNone
LanguageHaskell2010

Network.Top.Util

Contents

Synopsis

Exceptions

strictTry :: NFData a => IO a -> IO (Either SomeException a) Source #

Strict try, deepseqs the returned value

try :: Exception e => IO a -> IO (Either e a) #

Similar to catch, but returns an Either result which is (Right a) if no exception of type e was raised, or (Left ex) if an exception of type e was raised and its value is ex. If any other type of exception is raised than it will be propogated up to the next enclosing exception handler.

 try a = catch (Right `liftM` a) (return . Left)

tryE :: IO a -> IO (Either SomeException a) Source #

Like try but with returned exception fixed to SomeException

forceE :: Either String c -> c Source #

forceE == either error id

data SomeException :: * #

The SomeException type is the root of the exception type hierarchy. When an exception of type e is thrown, behind the scenes it is encapsulated in a SomeException.

Time

milliseconds :: Num a => a -> a Source #

Convert milliseconds to microseconds (μs)

seconds :: Num a => a -> a Source #

Convert seconds to microseconds (μs)

minutes :: Num c => c -> c Source #

Convert minutes to microseconds (μs)

withTimeout Source #

Arguments

:: Int

Timeout (in seconds)

-> IO a

Op to execute

-> IO (Either String a)

Right if op completed correctly, Left otherwise withTimeout secs op = maybe (Left Timeout) Right $ timeout (seconds secs) op

Run an IO op with a timeout

Threads

async :: IO a -> IO (Async a) #

Spawn an asynchronous action in a separate thread.

cancel :: Async a -> IO () #

Cancel an asynchronous action by throwing the ThreadKilled exception to it, and waiting for the Async thread to quit. Has no effect if the Async has already completed.

cancel a = throwTo (asyncThreadId a) ThreadKilled <* waitCatch a

Note that cancel will not terminate until the thread the Async refers to has terminated. This means that cancel will block for as long said thread blocks when receiving an asynchronous exception.

For example, it could block if:

  • It's executing a foreign call, and thus cannot receive the asynchronous exception;
  • It's executing some cleanup handler after having received the exception, and the handler is blocking.

threadDelay :: Int -> IO () #

Suspends the current thread for a given number of microseconds (GHC only).

There is no guarantee that the thread will be rescheduled promptly when the delay has expired, but the thread will never continue to run earlier than specified.

Monads

liftIO :: MonadIO m => forall a. IO a -> m a #

Lift a computation from the IO monad.

forever :: Applicative f => f a -> f b #

forever act repeats the action infinitely.

when :: Applicative f => Bool -> f () -> f () #

Conditional execution of Applicative expressions. For example,

when debug (putStrLn "Debugging")

will output the string Debugging if the Boolean value debug is True, and otherwise do nothing.

unless :: Applicative f => Bool -> f () -> f () #

The reverse of when.

Logging (with native ghcjs support)

dbg :: MonadIO m => [String] -> m () Source #

Log multiple messages at DEBUG level

warn :: MonadIO m => [String] -> m () Source #

Log multiple messages at WARNING level

info :: MonadIO m => [String] -> m () Source #

Log multiple messages at INFO level

err :: MonadIO m => [String] -> m () Source #

Log multiple messages at ERROR level

dbgS :: String -> IO () Source #

Log a message at DEBUG level

logLevel :: Priority -> IO () Source #

Setup the global logging level

Other

eitherToMaybe :: Either t a -> Maybe a Source #

Convert an Either to a Maybe