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
(MkJsCallback fn) => CanPassToJavaScript (JsCallback fn)
#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 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
mkJsCallback :: fn -> Int -> ParseM (Effect Unit)
Instances
MkJsCallback (Effect Unit)
(CanReceiveFromJavaScript a, MkJsCallback b) => MkJsCallback (a -> b)
#jsCallback Source
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' Source
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 Source
jsCallback0 :: Effect Unit -> JsCallback0
Deprecated. Same as jsCallback
.
This is the internal implementation of
jsCallback
andjsCallback'
. Do not use directly.