1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

ui: Refactor update and setRoute with updateWith

This commit is contained in:
James Hillyerd
2018-11-22 22:02:01 -08:00
parent 0c9cf81c94
commit e71377f966
5 changed files with 69 additions and 87 deletions

View File

@@ -39,7 +39,7 @@ init sessionValue location key =
session =
Session.init key location (Session.decodeValueWithDefault sessionValue)
( subModel, _ ) =
( subModel, _, _ ) =
Home.init
model =
@@ -51,7 +51,7 @@ init sessionValue location key =
route =
Route.fromUrl location
in
applySession (setRoute route model)
changeRouteTo route model |> updateSession
type Msg
@@ -106,7 +106,7 @@ pageSubscriptions page =
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
applySession <|
updateSession <|
case msg of
LinkClicked req ->
case req of
@@ -119,7 +119,7 @@ update msg model =
UrlChanged url ->
-- Responds to new browser URL.
if model.session.routing then
setRoute (Route.fromUrl url) model
changeRouteTo (Route.fromUrl url) model
else
-- Skip once, but re-enable routing.
@@ -162,36 +162,30 @@ update msg model =
-}
updatePage : Msg -> Model -> ( Model, Cmd Msg, Session.Msg )
updatePage msg model =
let
-- Handles sub-model update by calling toUpdate with subMsg & subModel, then packing the
-- updated sub-model back into model.page.
modelUpdate toPage toMsg subUpdate subMsg subModel =
let
( newModel, subCmd, sessionMsg ) =
subUpdate model.session subMsg subModel
in
( { model | page = toPage newModel }, Cmd.map toMsg subCmd, sessionMsg )
in
case ( msg, model.page ) of
( HomeMsg subMsg, Home subModel ) ->
modelUpdate Home HomeMsg Home.update subMsg subModel
Home.update model.session subMsg subModel
|> updateWith Home HomeMsg model
( MailboxMsg subMsg, Mailbox subModel ) ->
modelUpdate Mailbox MailboxMsg Mailbox.update subMsg subModel
Mailbox.update model.session subMsg subModel
|> updateWith Mailbox MailboxMsg model
( MonitorMsg subMsg, Monitor subModel ) ->
modelUpdate Monitor MonitorMsg Monitor.update subMsg subModel
Monitor.update model.session subMsg subModel
|> updateWith Monitor MonitorMsg model
( StatusMsg subMsg, Status subModel ) ->
modelUpdate Status StatusMsg Status.update subMsg subModel
Status.update model.session subMsg subModel
|> updateWith Status StatusMsg model
( _, _ ) ->
-- Disregard messages destined for the wrong page.
( model, Cmd.none, Session.none )
setRoute : Route -> Model -> ( Model, Cmd Msg, Session.Msg )
setRoute route model =
changeRouteTo : Route -> Model -> ( Model, Cmd Msg, Session.Msg )
changeRouteTo route model =
let
( newModel, newCmd, newSession ) =
case route of
@@ -199,50 +193,24 @@ setRoute route model =
( model, Cmd.none, Session.SetFlash ("Unknown route requested: " ++ hash) )
Route.Home ->
let
( subModel, subCmd ) =
Home.init
in
( { model | page = Home subModel }
, Cmd.map HomeMsg subCmd
, Session.none
)
Home.init
|> updateWith Home HomeMsg model
Route.Mailbox name ->
let
( subModel, subCmd ) =
Mailbox.init name Nothing
in
( { model | page = Mailbox subModel }
, Cmd.map MailboxMsg subCmd
, Session.none
)
Mailbox.init name Nothing
|> updateWith Mailbox MailboxMsg model
Route.Message mailbox id ->
let
( subModel, subCmd ) =
Mailbox.init mailbox (Just id)
in
( { model | page = Mailbox subModel }
, Cmd.map MailboxMsg subCmd
, Session.none
)
Mailbox.init mailbox (Just id)
|> updateWith Mailbox MailboxMsg model
Route.Monitor ->
let
( subModel, subCmd ) =
Monitor.init
in
( { model | page = Monitor subModel }
, Cmd.map MonitorMsg subCmd
, Session.none
)
Monitor.init
|> updateWith Monitor MonitorMsg model
Route.Status ->
( { model | page = Status Status.init }
, Cmd.map StatusMsg Status.load
, Session.none
)
Status.init
|> updateWith Status StatusMsg model
in
case model.page of
Monitor _ ->
@@ -253,8 +221,8 @@ setRoute route model =
( newModel, newCmd, newSession )
applySession : ( Model, Cmd Msg, Session.Msg ) -> ( Model, Cmd Msg )
applySession ( model, cmd, sessionMsg ) =
updateSession : ( Model, Cmd Msg, Session.Msg ) -> ( Model, Cmd Msg )
updateSession ( model, cmd, sessionMsg ) =
let
session =
Session.update sessionMsg model.session
@@ -272,6 +240,21 @@ applySession ( model, cmd, sessionMsg ) =
)
{-| Map page updates to Main Model and Msg types.
-}
updateWith :
(subModel -> Page)
-> (subMsg -> Msg)
-> Model
-> ( subModel, Cmd subMsg, Session.Msg )
-> ( Model, Cmd Msg, Session.Msg )
updateWith toPage toMsg model ( subModel, subCmd, sessionMsg ) =
( { model | page = toPage subModel }
, Cmd.map toMsg subCmd
, sessionMsg
)
-- VIEW

View File

@@ -17,7 +17,7 @@ type alias Model =
{ greeting : String }
init : ( Model, Cmd Msg )
init : ( Model, Cmd Msg, Session.Msg )
init =
let
cmdGreeting =
@@ -26,7 +26,7 @@ init =
, expect = Http.expectString GreetingLoaded
}
in
( Model "", cmdGreeting )
( Model "", cmdGreeting, Session.none )

View File

@@ -77,10 +77,11 @@ type alias Model =
}
init : String -> Maybe MessageID -> ( Model, Cmd Msg )
init : String -> Maybe MessageID -> ( Model, Cmd Msg, Session.Msg )
init mailboxName selection =
( Model mailboxName (LoadingList selection) SafeHtmlBody "" (Time.millisToPosix 0)
, load mailboxName
, Session.none
)

View File

@@ -27,9 +27,9 @@ type MonitorMessage
| Message MessageHeader
init : ( Model, Cmd Msg )
init : ( Model, Cmd Msg, Session.Msg )
init =
( Model False [], Ports.monitorCommand True )
( Model False [], Ports.monitorCommand True, Session.none )

View File

@@ -1,4 +1,4 @@
module Page.Status exposing (Model, Msg, init, load, subscriptions, update, view)
module Page.Status exposing (Model, Msg, init, subscriptions, update, view)
import Data.Metrics as Metrics exposing (Metrics)
import Data.Session as Session exposing (Session)
@@ -46,25 +46,28 @@ type alias Metric =
}
init : Model
init : ( Model, Cmd Msg, Session.Msg )
init =
{ metrics = Nothing
, xCounter = 60
, sysMem = Metric "System Memory" 0 Filesize.format graphZero initDataSet 10
, heapSize = Metric "Heap Size" 0 Filesize.format graphZero initDataSet 10
, heapUsed = Metric "Heap Used" 0 Filesize.format graphZero initDataSet 10
, heapObjects = Metric "Heap # Objects" 0 fmtInt graphZero initDataSet 10
, goRoutines = Metric "Goroutines" 0 fmtInt graphZero initDataSet 10
, webSockets = Metric "Open WebSockets" 0 fmtInt graphZero initDataSet 10
, smtpConnOpen = Metric "Open Connections" 0 fmtInt graphZero initDataSet 10
, smtpConnTotal = Metric "Total Connections" 0 fmtInt graphChange initDataSet 60
, smtpReceivedTotal = Metric "Messages Received" 0 fmtInt graphChange initDataSet 60
, smtpErrorsTotal = Metric "Messages Errors" 0 fmtInt graphChange initDataSet 60
, smtpWarnsTotal = Metric "Messages Warns" 0 fmtInt graphChange initDataSet 60
, retentionDeletesTotal = Metric "Retention Deletes" 0 fmtInt graphChange initDataSet 60
, retainedCount = Metric "Stored Messages" 0 fmtInt graphZero initDataSet 60
, retainedSize = Metric "Store Size" 0 Filesize.format graphZero initDataSet 60
}
( { metrics = Nothing
, xCounter = 60
, sysMem = Metric "System Memory" 0 Filesize.format graphZero initDataSet 10
, heapSize = Metric "Heap Size" 0 Filesize.format graphZero initDataSet 10
, heapUsed = Metric "Heap Used" 0 Filesize.format graphZero initDataSet 10
, heapObjects = Metric "Heap # Objects" 0 fmtInt graphZero initDataSet 10
, goRoutines = Metric "Goroutines" 0 fmtInt graphZero initDataSet 10
, webSockets = Metric "Open WebSockets" 0 fmtInt graphZero initDataSet 10
, smtpConnOpen = Metric "Open Connections" 0 fmtInt graphZero initDataSet 10
, smtpConnTotal = Metric "Total Connections" 0 fmtInt graphChange initDataSet 60
, smtpReceivedTotal = Metric "Messages Received" 0 fmtInt graphChange initDataSet 60
, smtpErrorsTotal = Metric "Messages Errors" 0 fmtInt graphChange initDataSet 60
, smtpWarnsTotal = Metric "Messages Warns" 0 fmtInt graphChange initDataSet 60
, retentionDeletesTotal = Metric "Retention Deletes" 0 fmtInt graphChange initDataSet 60
, retainedCount = Metric "Stored Messages" 0 fmtInt graphZero initDataSet 60
, retainedSize = Metric "Store Size" 0 Filesize.format graphZero initDataSet 60
}
, getMetrics
, Session.none
)
initDataSet : Spark.DataSet
@@ -73,11 +76,6 @@ initDataSet =
|> List.map (\x -> ( toFloat x, 0 ))
load : Cmd Msg
load =
getMetrics
-- SUBSCRIPTIONS --