billstclair / elm-port-funnel / PortFunnel.Echo

An example echo funnel, with a simulator.

Types


type Message
    = Request String
    | Startup

Since this is a simple echo example, the messages are just strings.


type Response
    = NoResponse
    | MessageResponse Message
    | CmdResponse Message
    | ListResponse (List Response)

A MessageResponse encapsulates a message.

NoResponse is currently unused, but many PortFunnel-aware modules will need it.

CmdResponse denotes a message that needs to be sent through the port. This is done by the commander function.

ListResponse allows us to return multiple responses. commander descends a ListResponse looking for CmdResponse responses. findMessages descends a list of Response records, collecting the MessageResponse messages.


type State

Our internal state.

Just tracks all incoming messages.

Components of a PortFunnel.FunnelSpec

moduleName : String

The name of this module: "Echo".

moduleDesc : PortFunnel.ModuleDesc Message State Response

Our module descriptor.

commander : (PortFunnel.GenericMessage -> Platform.Cmd.Cmd msg) -> Response -> Platform.Cmd.Cmd msg

Responsible for sending a CmdResponse back througt the port.

Called by PortFunnel.appProcess for each response returned by process.

Initial State

initialState : State

The initial, empty state, so the application can initialize its state.

Sending a Message out the Cmd Port

makeMessage : String -> Message

Make a message to send out through the port.

send : (Json.Encode.Value -> Platform.Cmd.Cmd msg) -> Message -> Platform.Cmd.Cmd msg

Send a Message through a Cmd port.

Conversion to Strings

toString : Message -> String

Convert a Message to a nice-looking human-readable string.

toJsonString : Message -> String

Convert a Message to the same JSON string that gets sent

over the wire to the JS code.

Simulator

makeSimulatedCmdPort : (Json.Encode.Value -> msg) -> Json.Encode.Value -> Platform.Cmd.Cmd msg

Make a simulated Cmd port.

Non-standard Functions

isLoaded : State -> Basics.Bool

Returns true if a Startup message has been processed.

This is sent by the port code after it has initialized.

findMessages : List Response -> List Message

When it needs to send the tail of a message beginning with a dollar sign

through the port, the Echo module returns a ListResponse. This function recursively descends a ListResponse, and returns a list of the Messages from any MessageResponses it finds.

stateToStrings : State -> List String

Convert our State to a list of strings.