我如何获得Elm 0.17 / 0.18的当前时间? [英] How do I get the current time in Elm 0.17/0.18?
问题描述
我已经问过这个问题了:
我如何在Elm中获得当前时间?
通过编写我自己的(现在不推荐使用)来回答它。 start-app的变体:
http://package.elm-lang.org/packages/z5h/time-app/1.0.1
当然Elm架构已经发生了变化,而且由于没有信号或 Time.timestamp
。
所以....
假设我使用标准更新函数签名构建应用程序:
更新:消息 - >型号 - > (Model,Cmd Msg)
我想用更新的时间为我的模型添加时间戳。一个不可接受几乎解决方案是订阅 Time.every
。从概念上讲,这不是我想要的。这是随着时间的推移更新模型,也是用消息分别更新模型。
我想要的是能够使用签名编写更新函数:
updateWithTime:Msg - >时间 - >型号 - > (Model,Cmd Msg)
我开始尝试通过添加一些额外的消息:
Msg = ...当|时NewTime Time
然后创建一个新的命令:
timeCmd = perform(\x - > NewTime 0.0)NewTime Time.now
因此,在任何操作中,我都可以触发额外的命令来检索时间。但是这会变得混乱和迅速失控。
有关我如何清理这个问题的任何想法?
一个选项无需在每个更新路径上执行时间获取,都可以将您的 Msg
包装为另一种消息类型那会获取时间,然后用时间调用正常的 update
。这是 http://elm-lang.org/examples/buttons 的修改版本,它将每次更新都会更新模型上的时间戳。
$ b
import Html exposed(div,button,text )
导入Html.App公开(程序)
导入Html.Events公开(onClick)
导入任务
导入时间公开(时间)
main =
program {init =(Model 0 0,Cmd.none),view = view,update = update,subscriptions =(\_ - > Sub.none)}
类型别名Model =
{count:Int
,updateTime:Time
}
view model =
Html.App.map GetTimeAndThen( modelView模型)
类型消息
= GetTimeAndThen ModelMsg
| GotTime ModelMsg时间
更新msg模型=
个案消息
GetTimeAndThen wrappedMsg - >
(model,Task.perform(\_-> Debug.crash)(GotTime wrappedMsg)Time.now)
GotTime wrappedMsg time - >
let
(newModel,cmd)= modelUpdate wrappedMsg时间模型
in
(newModel,Cmd.map GetTimeAndThen cmd)
type ModelMsg = Increment |减量
型号更新消息时间型号=
消息消息
增量 - >
({model | count = model.count + 1,updateTime = time},Cmd.none)
递减 - >
({model | count = model.count - 1,updateTime = time},Cmd.none)
modelView模型=
div []
[button [ onClick Decrement] [text] - ]
,div [] [text(toString model.count)]
,button [onClick Increment] [text+]
,div [] [text(toString model.updateTime)]
]
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-app:
http://package.elm-lang.org/packages/z5h/time-app/1.0.1
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
.
So....
Suppose I build an app with the standard update function signature:
update : Msg -> Model -> (Model, Cmd Msg)
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.
What I want is to be able to write an update function with signature:
updateWithTime : Msg -> Time -> Model -> (Model, Cmd Msg)
I started trying to solve this by adding some extra messages:
Msg = ... When | NewTime Time
And creating a new command:
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?
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屋!