module Update.HandleServerReply exposing (apply_to) -- Elm ------------------------------------------------------------------------- import Http -- Shared ---------------------------------------------------------------------- import Struct.Flags -- Main Menu ------------------------------------------------------------------- import Struct.Error import Struct.Event import Struct.Model import Struct.Player import Struct.ServerReply -------------------------------------------------------------------------------- -- TYPES ----------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- apply_command : ( Struct.ServerReply.Type -> (Struct.Model.Type, (List (Cmd Struct.Event.Type))) -> (Struct.Model.Type, (List (Cmd Struct.Event.Type))) ) apply_command command current_state = case command of Struct.ServerReply.Okay -> current_state -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- apply_to : ( Struct.Model.Type -> (Result Http.Error (List Struct.ServerReply.Type)) -> (Struct.Model.Type, (Cmd Struct.Event.Type)) ) apply_to model query_result = case query_result of (Result.Err error) -> ( (Struct.Model.invalidate (Struct.Error.new Struct.Error.Networking (toString error)) model ), Cmd.none ) (Result.Ok commands) -> let (new_model, elm_commands) = (List.foldl (apply_command) (model, [Cmd.none]) commands) in ( new_model, ( case elm_commands of [] -> Cmd.none [cmd] -> cmd _ -> (Cmd.batch elm_commands) ) )