如何在 Elm 0.17/0.18 中获取当前时间? [英] How do I get the current time in Elm 0.17/0.18?
问题描述
我已经问过这个问题了:
如何在 Elm 中获取当前时间?
I had asked this question already:
How do I get the current time in Elm?
并通过编写我自己的(现已弃用)start-app 变体来回答它:http://package.elm-lang.org/packages/z5h/time-app/1.0.1
And answered it by writing my own (now deprecated) variant of start-app:
http://package.elm-lang.org/packages/z5h/time-app/1.0.1
当然,Elm 架构已经改变了,我以前的做事方式不再有效,因为没有信号或 Time.timestamp
.
Of course the Elm architecture has since changed, and my old way of doing things no longer works, because there are no signals or Time.timestamp
.
所以....
假设我使用标准更新函数签名构建一个应用程序:
<代码>更新:消息->模型 ->(型号, Cmd Msg)
Suppose I build an app with the standard update function signature:
update : Msg -> Model -> (Model, Cmd Msg)
我想用更新时间为我的模型加上时间戳.一种不可接受的几乎解决方案是订阅Time.every
.从概念上讲,这不是我想要的.这是随时间更新模型,也分别用消息更新模型.
I'd like to timestamp my model with the time at update. One unacceptable almost-solution is to subscribe to Time.every
. Conceptually this is not what I want. This is updating the model with time and also separately updating model with messages.
我想要的是能够写一个带有签名的更新函数:updateWithTime : Msg ->时间 ->模型 ->(型号, Cmd Msg)
What I want is to be able to write an update function with signature:
updateWithTime : Msg -> Time -> Model -> (Model, Cmd Msg)
我开始尝试通过添加一些额外的消息来解决这个问题:Msg = ... 当 |新时间
I started trying to solve this by adding some extra messages:
Msg = ... When | NewTime Time
并创建一个新命令:timeCmd = perform (x -> NewTime 0.0) NewTime Time.now
因此,在任何操作中,我都可以发出一个额外的命令来检索时间.但这很快就会变得混乱和失控.
So in any action, I can fire off an extra command to retrieve the time. But this gets messy and out of hand quickly.
关于如何清理它的任何想法?
Any ideas on how I can clean this up?
推荐答案
无需在每个更新路径上进行时间获取的一种选择是将您的 Msg
包装在另一种可以获取时间的消息类型中,然后使用时间.这是 http://elm-lang.org/examples/buttons 的修改版本,它将每次更新时更新模型的时间戳.
One option without having to do the time fetch on every update path would be to wrap your Msg
in another message type that would fetch the time and then call your normal update
with the time. This is a modified version of http://elm-lang.org/examples/buttons that will update a timestamp on the model with every update.
import Html exposing (div, button, text)
import Html.App exposing (program)
import Html.Events exposing (onClick)
import Task
import Time exposing (Time)
main =
program { init = (Model 0 0, Cmd.none), view = view, update = update, subscriptions = (\_ -> Sub.none) }
type alias Model =
{ count: Int
, updateTime : Time
}
view model =
Html.App.map GetTimeAndThen (modelView model)
type Msg
= GetTimeAndThen ModelMsg
| GotTime ModelMsg Time
update msg model =
case msg of
GetTimeAndThen wrappedMsg ->
(model, Task.perform (\_ -> Debug.crash "") (GotTime wrappedMsg) Time.now)
GotTime wrappedMsg time ->
let
(newModel, cmd) = modelUpdate wrappedMsg time model
in
(newModel, Cmd.map GetTimeAndThen cmd)
type ModelMsg = Increment | Decrement
modelUpdate msg time model =
case msg of
Increment ->
({model | count = model.count + 1, updateTime = time}, Cmd.none)
Decrement ->
({model | count = model.count - 1, updateTime = time}, Cmd.none)
modelView model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model.count) ]
, button [ onClick Increment ] [ text "+" ]
, div [] [ text (toString model.updateTime) ]
]
这篇关于如何在 Elm 0.17/0.18 中获取当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!