Effects.tick replacement for elm 0.17

梦想的初衷 提交于 2019-12-24 15:39:22

问题


As it's stated in the upgrade guide, Effects is being replaced by this new Applicative Functor-like thing Cmd. I don't see any trace of a clue as to where Effects.tick might be hiding, or how it could be reimplemented.

From the looks of things, Process.sleep might be the right answer, something like

Task.perform errorHandler (\x -> x) <| Process.sleep
                                    <| 500 * Time.millisecond

would allow the process to wait 500 milliseconds before issuing the next message / action. I'm just not sure if this is what will replace Effects.tick in the long run though.


回答1:


Effect.tick functionality is replaced by AnimationFrame.

You basically subscribe to a set of msg of either times or diffs. And react accordingly.

import Html exposing (..)
import Html.App as App 
import AnimationFrame 
import Time exposing (Time, second)

main = 
  App.program 
    { init = Model 0 0 ! []
    , update = \msg model -> update msg model ! []
    , view = view 
    , subscriptions = \_ -> AnimationFrame.diffs identity}

type alias Model = 
  { timeSinceLastIncrement : Time 
  , counter : Int }

incrementTime = 1*second

update diff {timeSinceLastIncrement, counter} = 
  if timeSinceLastIncrement > incrementTime then 
    Model 0 (counter+1)
  else
    Model (timeSinceLastIncrement+diff) counter

view {counter} = 
  div [] [text (toString counter)] 

I've chosen to send the Time diffs directly as messages and to unpack the structure of the model in both update and view for easier access to components. In a more complex app you will probably have something like a Tick Time message.



来源:https://stackoverflow.com/questions/37196357/effects-tick-replacement-for-elm-0-17

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!