This module allows to invoke JavaScript functions using the Elm's Task abstraction, which is convenient for chaining multiple API calls without introducing the complexity in the model of an Elm application.
Before TypePort can be used in Elm, it must be set up on JavaScript side. Refer to the README for comprehensive instructions.
Special version of the call that reduces amount of boilerplate code required when calling JavaScript functions
that don't take any parameters. It is eqivalent of passing Json.Encoder.null into the call.
type Msg = GotWidgetsCount (Result String Int)
TaskPort.callNoArgs "getWidgetsCount" Json.Decode.int Json.Decode.string
|> Task.attempt GotWidgetsCountA structured error describing exactly how the interop call failed. You can use this to determine the best way to react to and recover from the problem.
CallError prefix is for errors explicitly sent from the JavaScript side. The error information will be specific to the interop use case, and it should be reconsituted from a JSON payload.
InteropError prefix is for the failures of the interop mechanism itself.
Subcategory of errors indicating a failure of the interop mechanism itself. These errors are generally not receoverable, but you can use them to allow the application to fail gracefully, or at least provide useful context for debugging.
We are exposing tests suite to help test module's implementation details.
This is an embedded tests suite allowing to test module-internal logic. No need to use this.
Creates a Task encapsulating an invocation of a particular asyncronous JavaScript function. This function will usually be wrapped into a more specific one, which will partially apply it providing the encoder and the decoders but curry the last parameter, so that it could invoked where necessary as a
args -> Taskfunction.Because interop calls can fail, produced task would likely need to be piped into a
Task.attemptor handled further usingTask.onError.Here is a simple example that creates a
Cmdinvoking a registered JavaScript function calledpingand produces a messageGotPongwith aResult, containing either anOkvariant with a string (determined by the first decoder argument), or anErr, containing aTaskPort.Errordescribing what went wrong.The
Taskabstraction allows to effectively compose chains of tasks without creating many intermediate variants in the Msg type, and designing the model to deal with partially completed call chain. The following example shows how this might be used when working with a hypothetical 'chatty' JavaScript API, requiring to callgetWidgetsCountfunction to obtain a number of widgets, and then callgetWidgetNamewith each widget's index to obtain its name.The resulting task has type
Task (TaskPort.Error String) (List String), which could be attempted as a single command, which, if successful, provides a handyList Stringwith all widget names.