2020-06-13 18:58:15 +00:00
|
|
|
module Util.Update exposing (andThen1, andThen2)
|
2019-07-22 22:53:30 +00:00
|
|
|
|
|
|
|
|
2019-12-29 20:55:12 +00:00
|
|
|
andThen1 : List (a -> ( a, Cmd b )) -> a -> ( a, Cmd b )
|
2019-07-22 22:53:30 +00:00
|
|
|
andThen1 fs a =
|
|
|
|
let
|
2019-12-29 20:55:12 +00:00
|
|
|
init =
|
|
|
|
( a, [] )
|
|
|
|
|
2019-07-22 22:53:30 +00:00
|
|
|
update el tuple =
|
|
|
|
let
|
2019-12-29 20:55:12 +00:00
|
|
|
( a2, c2 ) =
|
|
|
|
el (Tuple.first tuple)
|
2019-07-22 22:53:30 +00:00
|
|
|
in
|
2019-12-29 20:55:12 +00:00
|
|
|
( a2, c2 :: Tuple.second tuple )
|
2019-07-22 22:53:30 +00:00
|
|
|
in
|
2019-12-29 20:55:12 +00:00
|
|
|
List.foldl update init fs
|
|
|
|
|> Tuple.mapSecond Cmd.batch
|
2020-06-13 18:58:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
andThen2 : List (model -> ( model, Cmd msg, Sub msg )) -> model -> ( model, Cmd msg, Sub msg )
|
|
|
|
andThen2 fs m =
|
|
|
|
let
|
|
|
|
init =
|
|
|
|
( m, [], [] )
|
|
|
|
|
|
|
|
update el ( m1, c1, s1 ) =
|
|
|
|
let
|
|
|
|
( m2, c2, s2 ) =
|
|
|
|
el m1
|
|
|
|
in
|
|
|
|
( m2, c2 :: c1, s2 :: s1 )
|
|
|
|
|
|
|
|
combine ( m1, cl, sl ) =
|
|
|
|
( m1, Cmd.batch cl, Sub.batch sl )
|
|
|
|
in
|
|
|
|
List.foldl update init fs
|
|
|
|
|> combine
|