如何获取 Elm 0.17/0.18 中的当前时间?
How do I get the current time in Elm 0.17/0.18?
我已经问过这个问题了:
并通过编写我自己的(现已弃用)start-app 变体来回答它:
http://package.elm-lang.org/packages/z5h/time-app/1.0.1
当然 Elm 架构已经改变了,我以前的做事方式不再有效,因为没有信号或 Time.timestamp
。
所以....
假设我使用标准更新函数签名构建一个应用程序:
update : Msg -> Model -> (Model, Cmd Msg)
我想用更新时间标记我的模型。一个 不可接受的 几乎解决方案是订阅 Time.every
。从概念上讲,这不是我想要的。这是随时间更新模型,也用消息单独更新模型。
我想要的是能够写一个带有签名的更新函数:
updateWithTime : Msg -> Time -> Model -> (Model, Cmd Msg)
我开始尝试通过添加一些额外的消息来解决这个问题:
Msg = ... When | NewTime Time
并创建一个新命令:
timeCmd = perform (\x -> NewTime 0.0) NewTime Time.now
所以在任何动作中,我都可以触发一个额外的命令来检索时间。但这很快就会变得混乱和失控。
关于如何清理它的任何想法?
无需在每个更新路径上执行时间获取的一个选项是将您的 Msg
包装在另一种消息类型中,该消息类型将获取时间,然后用时间调用您的正常 update
。这是 http://elm-lang.org/examples/buttons 的修改版本,每次更新都会更新模型上的时间戳。
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) ]
]
我发现我认为比公认的答案更优雅的解决方案。 GetTimeAndThen
消息没有两个单独的模型,而是包含一个 returns 消息的处理程序。代码感觉更自然,更像elm,可以更通用的方式使用:
module Main exposing (..)
import Html exposing (div, button, text)
import Html.App as App
import Html.Events exposing (onClick)
import Task
import Time exposing (Time)
main =
App.program
{ init = ( Model 0 0, Cmd.none )
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
view model =
div []
[ button [ onClick decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick increment ] [ text "+" ]
]
increment =
GetTimeAndThen (\time -> Increment time)
decrement =
GetTimeAndThen (\time -> Decrement time)
type Msg
= Increment Time
| Decrement Time
| GetTimeAndThen (Time -> Msg)
type alias Model =
{ count : Int, updateTime : Time }
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GetTimeAndThen successHandler ->
( model, (Task.perform assertNeverHandler successHandler Time.now) )
Increment time ->
( { model | count = model.count + 1, updateTime = time }, Cmd.none )
Decrement time ->
( { model | count = model.count - 1, updateTime = time }, Cmd.none )
assertNeverHandler : a -> b
assertNeverHandler =
(\_ -> Debug.crash "This should never happen")
在 Slack 上讨论了这个问题之后,这里有一个 Msg
中没有函数的替代实现。与接受的答案一样,模型仅在 Time.now
Task
成功时更新。
import Html exposing (div, button, text)
import Html.App as App
import Html.Events exposing (onClick)
import Task
import Time exposing (Time)
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick Increment ] [ text "+" ]
]
type Msg
= NoOp
| Increment
| Decrement
| GetTimeSuccess Msg Time
| GetTimeFailure String
type alias Model =
{ count : Int, updateTime : Result String Time }
init : (Model , Cmd Msg)
init =
( { count = 0
, updateTime = Err "No time yet!"
}
, Task.perform GetTimeFailure (GetTimeSuccess NoOp) Time.now
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp -> (model, Cmd.none)
Increment ->
( model
, Task.perform GetTimeFailure (GetTimeSuccess Increment) Time.now
)
Decrement ->
( model
, Task.perform GetTimeFailure (GetTimeSuccess Decrement) Time.now
)
GetTimeSuccess Increment time ->
( { model | count = model.count + 1, updateTime = Ok time}
, Cmd.none
)
GetTimeSuccess Decrement time ->
( { model | count = model.count - 1, updateTime = Ok time}
, Cmd.none
)
GetTimeSuccess _ time ->
( { model | updateTime = Ok time}
, Cmd.none
)
GetTimeFailure msg ->
( { model | updateTime = Err msg}
, Cmd.none
)
您可以创建一个本机模块,然后公开一个 timestamp
函数,该函数从 JavaScript 中的 Date.now()
获取时间。
大致是这样的:
Timestamp.elm
module Timestamp exposing (timestamp)
import Native.Timestamp
timestamp : () -> Int
timestamp a = Native.Timestamp.timestamp a
Native/Timestamp.js
var _YourRepoUserName$your_repo$Native_Timestamp = function() {
return { timestamp: function(a) {return Date.now()}
}
Main.elm
port module Main exposing (..)
import Timestamp exposing (timestamp)
然后您可以在 Elm 中的任何地方使用 (timestamp ()
) 来获取当前时间戳作为 Int。
注意:我使用了 timestamp : () -> Int
,因为否则我无法让它工作。 timestamp : Int
只是返回了第一次加载的硬编码时间。
让我知道是否可以改进。
我对自己的问题有一个答案(根据 amilner42 的建议)。我在当前代码中使用此解决方案。
我非常喜欢@w.brian 的解决方案,但是消息中的函数会破坏调试器。
我喜欢@robertjlooby 的解决方案,这非常相似,尽管它取消了额外的类型,并针对 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.
elm-0.18 完整示例https://runelm.io/c/72i
import Time exposing (Time)
import Html exposing (..)
import Html.Events exposing (onClick)
import Task
type Msg
= GetTime
| NewTime Time
type alias Model =
{ currentTime : Maybe Time
}
view : Model -> Html Msg
view model =
let
currentTime =
case model.currentTime of
Nothing ->
text ""
Just theTime ->
text <| toString theTime
in
div []
[ button [ onClick GetTime ] [ text "get time" ]
, currentTime
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GetTime ->
model ! [ Task.perform NewTime Time.now ]
NewTime time ->
{ model | currentTime = Just time } ! []
main : Program Never Model Msg
main =
program
{ init = init
, update = update
, view = view
, subscriptions = always Sub.none
}
init : ( Model, Cmd Msg )
init =
{ currentTime = Nothing } ! []
我已经问过这个问题了:
并通过编写我自己的(现已弃用)start-app 变体来回答它:
http://package.elm-lang.org/packages/z5h/time-app/1.0.1
当然 Elm 架构已经改变了,我以前的做事方式不再有效,因为没有信号或 Time.timestamp
。
所以....
假设我使用标准更新函数签名构建一个应用程序:
update : Msg -> Model -> (Model, Cmd Msg)
我想用更新时间标记我的模型。一个 不可接受的 几乎解决方案是订阅 Time.every
。从概念上讲,这不是我想要的。这是随时间更新模型,也用消息单独更新模型。
我想要的是能够写一个带有签名的更新函数:
updateWithTime : Msg -> Time -> Model -> (Model, Cmd Msg)
我开始尝试通过添加一些额外的消息来解决这个问题:
Msg = ... When | NewTime Time
并创建一个新命令:
timeCmd = perform (\x -> NewTime 0.0) NewTime Time.now
所以在任何动作中,我都可以触发一个额外的命令来检索时间。但这很快就会变得混乱和失控。
关于如何清理它的任何想法?
无需在每个更新路径上执行时间获取的一个选项是将您的 Msg
包装在另一种消息类型中,该消息类型将获取时间,然后用时间调用您的正常 update
。这是 http://elm-lang.org/examples/buttons 的修改版本,每次更新都会更新模型上的时间戳。
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) ]
]
我发现我认为比公认的答案更优雅的解决方案。 GetTimeAndThen
消息没有两个单独的模型,而是包含一个 returns 消息的处理程序。代码感觉更自然,更像elm,可以更通用的方式使用:
module Main exposing (..)
import Html exposing (div, button, text)
import Html.App as App
import Html.Events exposing (onClick)
import Task
import Time exposing (Time)
main =
App.program
{ init = ( Model 0 0, Cmd.none )
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
view model =
div []
[ button [ onClick decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick increment ] [ text "+" ]
]
increment =
GetTimeAndThen (\time -> Increment time)
decrement =
GetTimeAndThen (\time -> Decrement time)
type Msg
= Increment Time
| Decrement Time
| GetTimeAndThen (Time -> Msg)
type alias Model =
{ count : Int, updateTime : Time }
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GetTimeAndThen successHandler ->
( model, (Task.perform assertNeverHandler successHandler Time.now) )
Increment time ->
( { model | count = model.count + 1, updateTime = time }, Cmd.none )
Decrement time ->
( { model | count = model.count - 1, updateTime = time }, Cmd.none )
assertNeverHandler : a -> b
assertNeverHandler =
(\_ -> Debug.crash "This should never happen")
在 Slack 上讨论了这个问题之后,这里有一个 Msg
中没有函数的替代实现。与接受的答案一样,模型仅在 Time.now
Task
成功时更新。
import Html exposing (div, button, text)
import Html.App as App
import Html.Events exposing (onClick)
import Task
import Time exposing (Time)
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick Increment ] [ text "+" ]
]
type Msg
= NoOp
| Increment
| Decrement
| GetTimeSuccess Msg Time
| GetTimeFailure String
type alias Model =
{ count : Int, updateTime : Result String Time }
init : (Model , Cmd Msg)
init =
( { count = 0
, updateTime = Err "No time yet!"
}
, Task.perform GetTimeFailure (GetTimeSuccess NoOp) Time.now
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp -> (model, Cmd.none)
Increment ->
( model
, Task.perform GetTimeFailure (GetTimeSuccess Increment) Time.now
)
Decrement ->
( model
, Task.perform GetTimeFailure (GetTimeSuccess Decrement) Time.now
)
GetTimeSuccess Increment time ->
( { model | count = model.count + 1, updateTime = Ok time}
, Cmd.none
)
GetTimeSuccess Decrement time ->
( { model | count = model.count - 1, updateTime = Ok time}
, Cmd.none
)
GetTimeSuccess _ time ->
( { model | updateTime = Ok time}
, Cmd.none
)
GetTimeFailure msg ->
( { model | updateTime = Err msg}
, Cmd.none
)
您可以创建一个本机模块,然后公开一个 timestamp
函数,该函数从 JavaScript 中的 Date.now()
获取时间。
大致是这样的:
Timestamp.elm
module Timestamp exposing (timestamp)
import Native.Timestamp
timestamp : () -> Int
timestamp a = Native.Timestamp.timestamp a
Native/Timestamp.js
var _YourRepoUserName$your_repo$Native_Timestamp = function() {
return { timestamp: function(a) {return Date.now()}
}
Main.elm
port module Main exposing (..)
import Timestamp exposing (timestamp)
然后您可以在 Elm 中的任何地方使用 (timestamp ()
) 来获取当前时间戳作为 Int。
注意:我使用了 timestamp : () -> Int
,因为否则我无法让它工作。 timestamp : Int
只是返回了第一次加载的硬编码时间。
让我知道是否可以改进。
我对自己的问题有一个答案(根据 amilner42 的建议)。我在当前代码中使用此解决方案。
我非常喜欢@w.brian 的解决方案,但是消息中的函数会破坏调试器。
我喜欢@robertjlooby 的解决方案,这非常相似,尽管它取消了额外的类型,并针对 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.
elm-0.18 完整示例https://runelm.io/c/72i
import Time exposing (Time)
import Html exposing (..)
import Html.Events exposing (onClick)
import Task
type Msg
= GetTime
| NewTime Time
type alias Model =
{ currentTime : Maybe Time
}
view : Model -> Html Msg
view model =
let
currentTime =
case model.currentTime of
Nothing ->
text ""
Just theTime ->
text <| toString theTime
in
div []
[ button [ onClick GetTime ] [ text "get time" ]
, currentTime
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GetTime ->
model ! [ Task.perform NewTime Time.now ]
NewTime time ->
{ model | currentTime = Just time } ! []
main : Program Never Model Msg
main =
program
{ init = init
, update = update
, view = view
, subscriptions = always Sub.none
}
init : ( Model, Cmd Msg )
init =
{ currentTime = Nothing } ! []