Safe Haskell | None |
---|---|
Language | Haskell98 |
- serve :: Config -> (Window -> IO ()) -> IO ()
- data Config = Config {
- jsPort :: Maybe Int
- jsAddr :: Maybe ByteString
- jsCustomHTML :: Maybe FilePath
- jsStatic :: Maybe FilePath
- jsLog :: ByteString -> IO ()
- defaultConfig :: Config
- data Window
- root :: Window -> RemotePtr ()
- class ToJS a where
- class FromJS a
- data JSFunction a
- type JSObject = RemotePtr JSPtr
- class FFI a
- ffi :: FFI a => String -> a
- runFunction :: Window -> JSFunction () -> IO ()
- callFunction :: Window -> JSFunction a -> IO a
- data NewJSObject
- unsafeCreateJSObject :: Window -> JSFunction NewJSObject -> IO JSObject
- class IsHandler a
- exportHandler :: IsHandler a => Window -> a -> IO JSObject
- onDisconnect :: Window -> IO () -> IO ()
- debug :: Window -> String -> IO ()
- timestamp :: Window -> IO ()
Synopsis
A JavaScript foreign function interface (FFI).
This module implements a web server that communicates with a web browser and allows you to execute arbitrary JavaScript code on it.
Note: This module is used internally by the Graphics.UI.Threepenny library, but the types are not compatible. Use Foreign.JavaScript only if you want to roll your own interface to the web browser.
Server
:: Config | Configuration options. |
-> (Window -> IO ()) | Initialization whenever a client connects. |
-> IO () |
Run a Foreign.JavaScript server.
Configuration of a Foreign.JavaScript server.
Config | |
|
defaultConfig :: Config Source #
Default configuration.
Port from environment variable or 8023
,
listening on localhost
, no custom HTML, no static directory,
logging to stderr.
JavaScript FFI
Helper class for rendering Haskell values as JavaScript expressions.
Helper class for converting JavaScript values to Haskell values.
fromJS
data JSFunction a Source #
A JavaScript function with a given output type a
.
Functor JSFunction Source # | Change the output type of a |
FromJS b => FFI (JSFunction b) Source # | |
Helper class for making ffi
a variable argument function.
fancy
ffi :: FFI a => String -> a Source #
Simple JavaScript FFI with string substitution.
Inspired by the Fay language. https://github.com/faylang/fay/wiki
example :: String -> Int -> JSFunction String example = ffi "$(%1).prop('checked',%2)"
The ffi
function takes a string argument representing the JavaScript
code to be executed on the client.
Occurrences of the substrings %1
to %9
will be replaced by
subequent arguments.
The substring %%
in the original will be replaced by %
(character escape).
Note: Always specify a type signature! The types automate
how values are marshalled between Haskell and JavaScript.
The class instances for the FFI
class show which conversions are supported.
runFunction :: Window -> JSFunction () -> IO () Source #
Run a JavaScript function, but do not wait for a result.
callFunction :: Window -> JSFunction a -> IO a Source #
Call a JavaScript function and wait for the result.
data NewJSObject Source #
A mutable JavaScript object that has just been created. This a dummy type used for additional type safety.
unsafeCreateJSObject :: Window -> JSFunction NewJSObject -> IO JSObject Source #
Run a JavaScript function that creates a new object.
Return a corresponding JSObject
without waiting for the browser
to send a result.
WARNING: This function assumes that the supplied JavaScript code does, in fact, create an object that is new.
Helper class for exporting Haskell functions to JavaScript as event handlers.
convertArgs, handle
exportHandler :: IsHandler a => Window -> a -> IO JSObject Source #
Export a Haskell function as an event handler.
The result is a JavaScript Function
object that can be called
from JavaScript like a regular function. However,
the corresponding Haskell function will not be run immediately,
rather it will be added to the event queue and processed
like an event. In other words, this the Haskell code is only called
asynchronously.
WARNING: The event handler will be garbage collected unless you keep a reference to it on the Haskell side! Registering it with a JavaScript function will generally not keep it alive.