Module

Elmish.JsCallback

This module is semi-deprecated and currently unused within Elmish. It will probably be resurrected as an optional-validation library.

#JsCallback

newtype JsCallback (fn :: Type)

This type represents a function that has been wrapped in a way suitable for passing to JavaScript (including parameter validation). The primary use case for such callbacks is to pass them to JSX code for receiving DOM-generated events and turning them into UI messages. See MkJsCallback for more info and examples.

Instances

#JsCallback0

type JsCallback0 = JsCallback (Effect Unit)

A parameterless JsCallback

#JsCallbackError

data JsCallbackError

Represents an error that may occur as a result of JS code calling a functuion wrapped as JsCallback.

Instances

#MkJsCallback

class MkJsCallback fn  where

The core logic of jsCallback.

This type class has two instances below:

  • The instance fn ~ Effect Unit represents a parameterless callback.
  • The instance fn ~ MkJsCallback b => a -> b is recursive, so it represents a callback with one or more parameters.

Members

Instances

#jsCallback

jsCallback :: forall fn. MkJsCallback fn => fn -> JsCallback fn

Wraps a given effect fn (possibly with parameters) as a JS non-curried function with parameter type validation, making it suitable for passing to unsafe JS code.

This function should not (or at least rarely) be used directly. In normal scenarios, Elmish.Dispatch.handle should be used instead.

Example:

  -- PureScript:
  createElement' ffiComponent_
      { onSave: jsCallback $ Console.log "Save"
      , onCancel: jsCallback $ Console.log "Cancel"
      , onFoo: jsCallback \(bar::String) (baz::Int) ->
          Console.log $ "bar = " <> bar <> ", baz = " <> show baz
      }

 // JSX:
 export const FfiComponent = props =>
   <div>
     <button onClick={props.onSave}>Save</button>
     <button onClick={props.onCancel}>Cancel</button>
     <button onClick={() => props.onFoo("bar", 42)}>Foo</button>
   </div>

In this example, the parameters bar and baz will undergo validation at runtime to make sure they are indeed a String and an Int respectively, and an error will be issued if validation fails.

#jsCallback'

jsCallback' :: forall fn. MkJsCallback fn => fn -> (JsCallbackError -> Effect Unit) -> JsCallback fn

A more elaborate version of jsCallback, which takes an extra parameter

  • an effect to be performed in case of errors.

#jsCallback0

jsCallback0 :: Effect Unit -> JsCallback0

Deprecated. Same as jsCallback.

Modules