【问题标题】:Effects.tick replacement for elm 0.17elm 0.17 的 Effects.tick 替换
【发布时间】:2016-05-12 19:58:48
【问题描述】:

正如the upgrade guide 中所述,Effects 正在被这个新的 Applicative Functor-like 事物Cmd 所取代。我没有看到任何关于 Effects.tick 可能隐藏在哪里或如何重新实现它的线索。

从表面上看,Process.sleep 可能是正确的答案,类似于

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

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

【问题讨论】:

    标签: thread-sleep elm


    【解决方案1】:

    Effect.tick 功能被 AnimationFrame 取代。

    您基本上订阅了一组时间或差异的消息。并做出相应的反应。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 2016-09-12
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多