How do I get the current time in Elm 0.17/0.18?

前端 未结 6 1976
悲哀的现实
悲哀的现实 2020-12-14 21:00

I had asked this question already:
How do I get the current time in Elm?

And answered it by writing my own (now deprecated) variant of start-ap

6条回答
  •  无人及你
    2020-12-14 21:16

    I have an answer to my own question (based on a suggestion by amilner42). I'm using this solution in my current code.

    I very much like the solution by @w.brian, but functions in messages break the debugger.
    I like the solution by @robertjlooby, and this is is very similar, though it does away with an extra type, and is updated for 0.18.

    update : Msg -> Model -> ( Model, Cmd Msg )
    update msg model =
        case msg of
            NoOp ->
                model ! []
    
            TickThen msg ->
                model ! [ Task.perform (Tock msg) Time.now ]
    
            Tock msg time ->
                    updateTimeStampedModel msg { model | time = time }
    
            otherMsg ->
                update (TickThen msg) model
    
    
    updateTimeStampedModel : Msg -> Model -> ( Model, Cmd Msg )
    updateTimeStampedModel msg model =
        case msg of
            NoOp ->
                update msg model
    
            TickThen _ ->
                update msg model
    
            Tock _ _ ->
                update msg model
    
            -- ALL OTHER MESSAGES ARE HANDLED HERE, AND ARE CODED TO ASSUME model.time IS UP-TO-DATE.
    

提交回复
热议问题