NoRedInk/elm-sweet-poll/4.0.1

NOTE: this package is not compatible with Elm 0.19.1

SweetPoll

Configuration & Set up

init : Config data -> ( Model data, 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 : Decoder data -> String -> Config data

Default configuration for SweetPoll.

type alias Config data =
{ url : String
, decoder : Decoder data
, delay : Time
, samesBeforeDelay : Int
, delayMultiplier : Float
, maxDelay : Time
}
- 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 Model data

Private state of the SweetPoll component.

import SweetPoll


type alias Model =
    { sweetPollModel : SweetPoll.Model ServerData
    , data : ServerData
    , error : Maybe Http.Error
    }


type alias ServerData =
    {}

Working with responses

type Msg data
update : Msg data -> Model 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 =
{ sweetPollModel : Model data
, newData : Maybe data
, error : Maybe Error
, cmd : Cmd (Msg data)
}
  • sweetPollModel: the new state of the SweetPoll
  • newData: any new data received by the SweetPoll
  • error: any new Http error occurring in the current update cycle
  • cmd: a Cmd to keep the SweetPoll running