| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Native.Module
Contents
Description
Bindings to Lynx native modules.
Native modules are exposed to JavaScript through a single global
NativeModules object and support two call shapes:
- synchronous / void —
NativeModules.<module>.<method>(args…) - asynchronous —
NativeModules.<module>.<method>(args…, callback), where the native side invokescallbackwith the result.
N.B. Per the Lynx documentation, native modules can only be used on the
background thread (BTS). These are plain IO actions, so the caller is
responsible for running them on the BTS — e.g. from the update of an action
dispatched to the BTS with runOnBG, or a background-thread
subscription.
Synopsis
- callNativeModule :: MisoString -> MisoString -> [Value] -> IO ()
- callNativeModuleWith :: FromJSON result => MisoString -> MisoString -> [Value] -> (Either MisoString result -> IO ()) -> IO ()
- nativeModules :: IO JSVal
- getNativeModule :: MisoString -> IO JSVal
Combinators
Arguments
| :: MisoString | Module name |
| -> MisoString | Method name |
| -> [Value] | Arguments |
| -> IO () |
Invoke a synchronous (void-returning) native-module method.
callNativeModule "NativeLocalStorageModule" "setStorageItem" [ String "myKey", String "myValue" ]
N.B. must be run on the background thread (BTS).
Arguments
| :: FromJSON result | |
| => MisoString | Module name |
| -> MisoString | Method name |
| -> [Value] | Arguments (callback appended automatically) |
| -> (Either MisoString result -> IO ()) | Continuation invoked exactly once when the native callback fires |
| -> IO () |
Invoke a callback-based native-module method. The native result is decoded
via FromJSON and handed to the supplied continuation, which fires exactly
once — with if the native call errored or the result failed
to decode. Callers that block awaiting the continuation (e.g. via an
Left errorMVar) can therefore rely on it always firing,
instead of hanging forever on the error path.
callNativeModuleWith "NativeLocalStorageModule" "getStorageItem" [ String "myKey" ] (either (const Nothing) Just)
The callback is appended to args automatically.
N.B. must be run on the background thread (BTS).
Low-level handles
nativeModules :: IO JSVal Source #
The global Lynx NativeModules object.
N.B. only available on the background thread (BTS).
getNativeModule :: MisoString -> IO JSVal Source #
Look up a native module by name: NativeModules.<name>.
N.B. only available on the background thread (BTS).