miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Native.Module

Description

Bindings to Lynx native modules.

Native modules are exposed to JavaScript through a single global NativeModules object and support two call shapes:

  • synchronous / voidNativeModules.<module>.<method>(args…)
  • asynchronousNativeModules.<module>.<method>(args…, callback), where the native side invokes callback with 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

Combinators

callNativeModule Source #

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).

callNativeModuleWith Source #

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 Left error if the native call errored or the result failed to decode. Callers that block awaiting the continuation (e.g. via an MVar) 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).