Effects.tick替换为榆木0.17 [英] Effects.tick replacement for elm 0.17

查看:73
本文介绍了Effects.tick替换为榆木0.17的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级指南中,效果被此类似Applicative Functor的新事物 Cmd 所代替。对于 Effects.tick 可能隐藏在哪里,或如何重新实现它,我看不到任何线索。

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.

从外观上看, Process.sleep 可能是正确的答案,例如

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

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

将允许进程等待500毫秒,然后发出下一条消息/操作。我只是不确定从长远来看是否会替换 Effects.tick。

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.

推荐答案

Effect.tick功能已由AnimationFrame代替。

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)] 

我选择直接将时间差异作为消息发送并解压缩更新视图中模型的结构,以便于访问组件。在更复杂的应用中,您可能会收到类似 Tick Time 的消息。

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.

这篇关于Effects.tick替换为榆木0.17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆