Elm Language
ccapndave / elm-update-extraを使用して複雑な更新機能を作成する
サーチ…
前書き
ccapndave / elm-update-extraは、より複雑な更新機能を扱うのに役立つ非常に便利なパッケージです。
メッセージのリストを呼び出すメッセージ
sequence機能を使用すると、他のメッセージのリストを呼び出すメッセージを簡単に記述できます。メッセージのセマンティクスを扱うときに便利です。
例1:ゲームエンジンを作っていて、すべてのフレームで画面を更新する必要があります。
module Video exposing (..)
type Message = module Video exposing (..)
import Update.Extra exposing (sequence)
-- Model definition [...]
type Message
= ClearBuffer
| DrawToBuffer
| UpdateLogic
| Update
update : Message -> Model -> (Model, Cmd)
update msg model =
case msg of
ClearBuffer ->
-- do something
DrawToBuffer ->
-- do something
UpdateLogic ->
-- do something
Update ->
model ! []
|> sequence update [ ClearBuffer
, DrawToBuffer
, UpdateLogic]
andとのメッセージの連鎖
andThen関数は、更新呼び出しの合成を可能にします。パイプライン演算子( |> )と共に使用して更新プログラムを連鎖させることができます。
例:文書エディタを作成していて、文書に送信する各変更メッセージを保存するには、次のようにします。
import Update.Extra exposing (andThen)
import Update.Extra.Infix exposing (..)
-- type alias Model = [...]
type Message
= ModifyDocumentWithSomeSettings
| ModifyDocumentWithOtherSettings
| SaveDocument
update : Model -> Message -> (Model, Cmd)
update model msg =
case msg of
ModifyDocumentWithSomeSettings ->
-- make the modifications
(modifiedModel, Cmd.none)
|> andThen SaveDocument
ModifyDocumentWithOtherSettings ->
-- make other modifications
(modifiedModel, Cmd.none)
|> andThen SaveDocument
SaveDocument ->
-- save document code
Update.Extra.Infix exposing (..)もインポートすると、中置演算子を使用できる場合があります。
update : Model -> Message -> (Model, Cmd)
update model msg =
case msg of
ModifyDocumentWithSomeSettings ->
-- make the modifications
(modifiedModel, Cmd.none)
:> andThen SaveDocument
ModifyDocumentWithOtherSettings ->
-- make other modifications
(modifiedModel, Cmd.none)
:> SaveDocument
SaveDocument ->
-- save document code
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow