peterszerzo/elm-porter/2.1.2

NOTE: the latest version is 3.0.0

Porter.Multi

With Porter.Multi you can create requests that have specialized return values.

This is an advanced usage pattern of Porter. For most use-cases, using Porter itself should be sufficient. If, however, you have a tangible issue with expressing chaining and transformations with your port messages, read along :).

The setup is exactly the same as when using Porter normally. Just use the request and send commands that this module provides instead to create requests with specialized return types.

Mapping over responses and chaining requests is supported.

Send Messages

type alias Request req res a =
MultiRequest req res a

We can either:

  • Perform a single request, which will run the Porter Request and at the end turn the result into an a,
  • Perform multiple requests: At the end of this request, run a function on the output that generates the next request. Note that this is set up in such a way that only the type of the final request in the chain matters.
  • Short-circuit; just pass on the value a without doing any more requests.
request : req -> (res -> a) -> Request req res a

Creates a new Porter.Multi request, specifying:

  • the request itself in the common req type
  • A response handler function tha turns the common res type into a specialized a.
send : Config req res msg -> (a -> msg) -> Request req res a -> Cmd msg

Actually sends a (chain of) request(s).

A final handler needs to be specified that turns the final result into a msg. This msg will be called with the final resulting a once the final response has returned.

fromSimple : Request req res -> Request req res res

Turns a simple Porter request into a Porter.Multi request

Chain Requests

andThen : (a -> Request req res b) -> Request req res a -> Request req res b

Combines together multiple Porter.Multi requests

(a -> Request req res (Result err b))
-> Request req res (Result err a)
-> Request req res (Result err b)

andThenResult function similar to andThen, but will short-circuit the request/response chain once an Err val is received.

Working with Result-objects is very common, since the specialising of res -> a (that you pass to request) is usually an operation that might fail (like a Decoder)

map : (a -> b) -> Request req res a -> Request req res b

Turns the request's specialized response type into a different type.

(a -> b -> c)
-> Request req res a
-> Request req res b
-> Request req res c

Chains two requests, and then combines their two responses into one new c.

(a -> b -> c -> d)
-> Request req res a
-> Request req res b
-> Request req res c
-> Request req res d

Chains three requests, and then combines their three responses into one new c.