Module

Elmish.JsCallback

Package
purescript-elmish
Repository
collegevine/purescript-elmish

#JsCallback Source

newtype JsCallback fn

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 Source

type JsCallback0 = JsCallback (Effect Unit)

A parameterless JsCallback

#JsCallbackError Source

data JsCallbackError

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

Instances

#MkJsCallback Source

class MkJsCallback fn  where

The core logic of mkJsCallback.

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

#mkJsCallback Source

mkJsCallback :: forall fn. MkJsCallback fn => fn -> (JsCallbackError -> Effect Unit) -> 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' theView_
      { onSave: jsCallback0 $ Console.log "Save"
      , onCancel: jsCallback0 $ Console.log "Cancel"
      , onFoo: mkJsCallback
           (\(bar::Int) (baz::Int) ->
                Console.log $ "bar = " <> show bar <> ", baz = " <> show baz
           )
           (\err -> pure unit {- ignore errors -})
      }

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

In this example, the parameters bar and baz will undergo validation at runtime, and an error will be issued if validation fails.

#jsCallback0 Source

jsCallback0 :: Effect Unit -> JsCallback0

A wrapper for mkJsCallback (see comments there)