Skip to content

Instantly share code, notes, and snippets.

@Janiczek
Created November 25, 2021 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Janiczek/1df19fb1dbe18d761855af9f213a86ee to your computer and use it in GitHub Desktop.
Save Janiczek/1df19fb1dbe18d761855af9f213a86ee to your computer and use it in GitHub Desktop.
main : Program Decode.Value Model_ Msg
main =
Browser.application
{ init = init_
, update = update_
, view = view_
, subscriptions = subscriptions
, onUrlRequest = UrlRequested
, onUrlChange = UrlChanged
}
type alias Model_ =
Result Error Model
type Error
= InitializationError Decode.Error
type alias Model =
{ flags : Flags
, url : Url
, navKey : Browser.Navigation.Key
}
type Msg
= NoOp
init_ : Decode.Value -> Url -> Browser.Navigation.Key -> ( Model_, Cmd Msg )
init_ flagsJson url key =
case Decode.decodeValue Flags.decoder flagsJson of
Ok flags ->
init flags url key
|> Tuple.mapFirst Ok
Err err ->
( Err <| InitializationError err
, Cmd.none
)
init : Flags -> Url -> Browser.Navigation.Key -> ( Model, Cmd Msg )
init flags url navKey =
( { flags = flags
, url = url
, navKey = navKey
}
, Cmd.none
)
subscriptions : Model_ -> Sub Msg
subscriptions model_ =
case model_ of
Err (InitializationError _) ->
Sub.none
Ok model ->
Sub.none
update_ : Msg -> Model_ -> ( Model_, Cmd Msg )
update_ msg model_ =
case model_ of
Err (InitializationError _) ->
( model_, Cmd.none )
Ok model ->
update msg model
|> Tuple.mapFirst Ok
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
view_ : Model_ -> Browser.Document Msg
view_ model_ =
{ title = "DevShell"
, body =
case model_ of
Err (InitializationError err) ->
[ Html.text <| "DevShell initialization error: " ++ Decode.errorToString err ]
Ok model ->
view model
}
view : Model -> List (Html Msg)
view model =
-- happy path, works on Model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment