Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
JSM monad keeps track of the JavaScript context
Synopsis
- newtype JSM a = JSM {
- unJSM :: ReaderT JSContextRef IO a
- data JSContextRef
- class (Applicative m, MonadIO m) => MonadJSM m
- liftJSM :: MonadJSM m => JSM a -> m a
- askJSM :: MonadJSM m => m JSContextRef
- runJSM :: MonadIO m => JSM a -> JSContextRef -> m a
- runJSaddle :: MonadIO m => JSContextRef -> JSM a -> m a
- syncPoint :: JSM ()
- syncAfter :: JSM a -> JSM a
- waitForAnimationFrame :: JSM Double
- nextAnimationFrame :: (Double -> JSM a) -> JSM a
- catch :: (MonadCatch m, HasCallStack, Exception e) => m a -> (e -> m a) -> m a
- bracket :: (HasCallStack, MonadMask m) => m a -> (a -> m c) -> (a -> m b) -> m b
Types
The JSM
monad keeps track of the JavaScript execution context.
When using GHCJS it is IO
.
Given a JSM
function and a JSContextRef
you can run the
function like this...
runJSM jsmFunction javaScriptContext
JSM | |
|
Instances
data JSContextRef Source #
Identifies a JavaScript execution context.
When using GHCJS this is just ()
since their is only one context.
When using GHC it includes the functions JSaddle needs to communicate
with the JavaScript context.
class (Applicative m, MonadIO m) => MonadJSM m Source #
Instances
MonadJSM JSM Source # | |
MonadJSM m => MonadJSM (MaybeT m) Source # | |
MonadJSM m => MonadJSM (ExceptT e m) Source # | |
MonadJSM m => MonadJSM (IdentityT m) Source # | |
MonadJSM m => MonadJSM (ReaderT r m) Source # | |
MonadJSM m => MonadJSM (StateT s m) Source # | |
MonadJSM m => MonadJSM (StateT s m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (WriterT w m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (WriterT w m) Source # | |
MonadJSM m => MonadJSM (ContT r m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (RWST r w s m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (RWST r w s m) Source # | |
Running JavaScript in a JavaScript context
askJSM :: MonadJSM m => m JSContextRef Source #
Gets the JavaScript context from the monad
runJSM :: MonadIO m => JSM a -> JSContextRef -> m a Source #
Runs a JSM
JavaScript function in a given JavaScript context.
runJSaddle :: MonadIO m => JSContextRef -> JSM a -> m a Source #
Alternative version of runJSM
Syncronizing with the JavaScript context
waitForAnimationFrame :: JSM Double Source #
On GHCJS this is waitForAnimationFrame
.
On GHC it will delay the execution of the current batch of asynchronous
command when they are sent to JavaScript. It will not delay the Haskell
code execution. The time returned will be based on the Haskell clock
(not the JavaScript clock).
nextAnimationFrame :: (Double -> JSM a) -> JSM a Source #
Tries to executes the given code in the next animation frame callback. Avoid synchronous opperations where possible.
Exception Handling
catch :: (MonadCatch m, HasCallStack, Exception e) => m a -> (e -> m a) -> m a #
Provide a handler for exceptions thrown during execution of the first
action. Note that type of the type of the argument to the handler will
constrain which exceptions are caught. See Control.Exception's
catch
.
bracket :: (HasCallStack, MonadMask m) => m a -> (a -> m c) -> (a -> m b) -> m b #
Generalized abstracted pattern of safe resource acquisition and release
in the face of errors. The first action "acquires" some value, which
is "released" by the second action at the end. The third action "uses"
the value and its result is the result of the bracket
.
If an error is thrown during the use, the release still happens before the error is rethrown.
Note that this is essentially a type-specialized version of
generalBracket
. This function has a more common signature (matching the
signature from Control.Exception), and is often more convenient to use. By
contrast, generalBracket
is more expressive, allowing us to implement
other functions like bracketOnError
.