billstclair / elm-mastodon-websocket / Mastodon.PortFunnels

Port Funnels

This is the interface to the JavaScript code for websockets and localStorage.

Your application needs to provide the actual ports, or simulators. See the example for details, and how to initialize the JavaScript code for the ports.

Types


type alias FunnelDict model msg =
( Dict String (Funnel model msg)
, State msg -> String -> model -> Json.Encode.Value -> Platform.Cmd.Cmd msg 
)

A Dict that maps a module name to a concretized FunnelSpec.

Create one with makeFunnelDict. Pass it to processValue.


type Handler model msg
    = LocalStorageHandler (PortFunnel.LocalStorage.Response -> State msg -> model -> ( model, Platform.Cmd.Cmd msg ))
    | WebSocketHandler (PortFunnel.WebSocket.Response -> State msg -> model -> ( model, Platform.Cmd.Cmd msg ))

A Handler tags a function to handle responses from one funnel module.

Add a tag in this type for each funnel module you use.


type alias State msg =
{ storage : PortFunnel.LocalStorage.State
, websocket : PortFunnel.WebSocket.State
, cmdPort : Json.Encode.Value -> Platform.Cmd.Cmd msg
, subPort : (Json.Encode.Value -> msg) -> Platform.Sub.Sub msg 
}

Add a property to this type for each funnel module you use.

Initialization

getCmdPort : State msg -> (Json.Encode.Value -> msg) -> String -> Basics.Bool -> Json.Encode.Value -> Platform.Cmd.Cmd msg

Turn the moduleName inside a GenericMessage into the output port.

getCmdPort tagger moduleName useSimulator

tagger is the same Msg that processes input from the subscription port.

moduleName will be ignored if useSimulator is False.

subscriptions : State msg -> (Json.Encode.Value -> msg) -> model -> Platform.Sub.Sub msg

Create a subscription for the subPort, given a Msg wrapper.

initialState : { localStoragePrefix : String, cmdPort : Json.Encode.Value -> Platform.Cmd.Cmd msg, subPort : (Json.Encode.Value -> msg) -> Platform.Sub.Sub msg } -> State msg

Create the initial state record.

makeFunnelDict : List (Handler model msg) -> (State msg -> String -> model -> Json.Encode.Value -> Platform.Cmd.Cmd msg) -> FunnelDict model msg

Make a Dict mapping moduleName to tagged concrete FunnelSpec.

The Handler list is a list of all of your handlers. E.g. for this example, it would be:

makeFunnelDict
    [ LocalStorageHandler localStorageHandler
    ]
    getCmdPort

Where echoHandler and addXYHandler are functions in your main application module to handle responses.

Processing

processValue : FunnelDict model msg -> Json.Encode.Value -> State msg -> model -> Result String ( model, Platform.Cmd.Cmd msg )

Process a value coming in through the subPort.

The FunnelDict is the result of calling makeFunnelDict.