billstclair/elm-port-funnel/1.1.4

NOTE: the latest version is 1.2.0

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.

= 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".

Our module descriptor.

commander : (GenericMessage -> Cmd msg) -> Response -> Cmd msg

Responsible for sending a CmdResponse back througt the port.

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

Initial 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 : (Value -> Cmd msg) -> Message -> 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 : (Value -> msg) -> Value -> Cmd msg

Make a simulated Cmd port.

Non-standard Functions

isLoaded : State -> Bool

Returns true if a Startup message has been processed.

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

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.