NoRedInk / elm-sweet-poll / SweetPoll

Configuration & Set up

init : Config data -> ( PollingState data, Platform.Cmd.Cmd (Msg data) )

Initialize the SweetPoll behavior.

import SweetPoll
import Json.Decode as Decode

init : (Model, Cmd Msg)
init =
    let
        (sweetPollModel, sweetPollCommands) =
            "www.example-url.com/get-some-data"
                |> SweetPoll.defaultConfig (Decode.succeed {})
                |> SweetPoll.init
                |> Tuple.mapSecond SweetPollMsg
    in
        ...

defaultConfig : Json.Decode.Decoder data -> String -> Config data

Default configuration for SweetPoll.


type alias Config data =
{ url : String
, decoder : Json.Decode.Decoder data
, delay : Basics.Float
, samesBeforeDelay : Basics.Int
, delayMultiplier : Basics.Float
, maxDelay : Basics.Float 
}



- url: Route to poll against
- decoder: How to handle the data you expect back
- delay: How long to wait between poll attempts
- samesBeforeDelay: How many identical responses before increasing the delay
- delayMultiplier: How much to multiply the delay by when getting identical responses
- maxDelay: How long the delay should be before we give up on polling


type PollingState data

Private state of the SweetPoll component.

import SweetPoll

type alias PollingState =
    { sweetPoll : SweetPoll.PollingState ServerData
    , data : ServerData
    , error : Maybe Http.Error
    }

type alias ServerData =
    {}

Working with responses


type Msg data

update : Config data -> Msg data -> PollingState data -> UpdateResult data

Takes the SweetPoll Msg and Model and produces a non-opaque result that you can work with.

import SweetPoll

type Msg
    = SweetPollMsg (SweetPoll.Msg ServerData)

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        SweetPollMsg sweetPollMsg ->
            case SweetPoll.update action model.sweetPollModel of
                { sweetPollModel, newData, error, cmd } ->
                    ( { sweetPollModel = sweetPollModel
                      , data = newData
                      , error = error
                      }
                    , cmd
                    )


type alias UpdateResult data =
{ newState : PollingState data
, newData : Maybe data
, error : Maybe Http.Error
, cmd : Platform.Cmd.Cmd (Msg data) 
}