Skip to content

Commit

Permalink
Add effects for status APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
jhillyerd committed Sep 12, 2020
1 parent de0d7cc commit c74bb6e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
52 changes: 40 additions & 12 deletions ui/src/Effect.elm
Expand Up @@ -5,6 +5,8 @@ module Effect exposing
, disableRouting
, enableRouting
, getGreeting
, getServerConfig
, getServerMetrics
, map
, none
, perform
Expand All @@ -13,6 +15,8 @@ module Effect exposing
)

import Api
import Data.Metrics exposing (Metrics)
import Data.ServerConfig exposing (ServerConfig)
import Data.Session as Session exposing (Session)


Expand All @@ -26,6 +30,8 @@ type Effect msg

type ApiEffect msg
= GetGreeting (Api.DataResult msg String)
| GetServerConfig (Api.DataResult msg ServerConfig)
| GetServerMetrics (Api.DataResult msg Metrics)


type SessionEffect
Expand Down Expand Up @@ -70,6 +76,12 @@ mapApi f effect =
GetGreeting result ->
GetGreeting <| result >> f

GetServerConfig result ->
GetServerConfig <| result >> f

GetServerMetrics result ->
GetServerMetrics <| result >> f


{-| Applies an effect by updating the session and/or producing a Cmd.
-}
Expand All @@ -87,15 +99,28 @@ perform ( session, effect ) =
Command cmd ->
( session, cmd )

SessionEffect sessionEffect ->
effectSession ( session, sessionEffect )

ApiEffect apiEffect ->
performApi ( session, apiEffect )

SessionEffect sessionEffect ->
performSession ( session, sessionEffect )


performApi : ( Session, ApiEffect msg ) -> ( Session, Cmd msg )
performApi ( session, effect ) =
case effect of
GetGreeting toMsg ->
( session, Api.getGreeting session toMsg )

GetServerConfig toMsg ->
( session, Api.getServerConfig session toMsg )

effectSession : ( Session, SessionEffect ) -> ( Session, Cmd msg )
effectSession ( session, effect ) =
GetServerMetrics toMsg ->
( session, Api.getServerMetrics session toMsg )


performSession : ( Session, SessionEffect ) -> ( Session, Cmd msg )
performSession ( session, effect ) =
case effect of
RecentAdd mailbox ->
( Session.addRecent mailbox session, Cmd.none )
Expand All @@ -113,13 +138,6 @@ effectSession ( session, effect ) =
( Session.enableRouting session, Cmd.none )


performApi : ( Session, ApiEffect msg ) -> ( Session, Cmd msg )
performApi ( session, effect ) =
case effect of
GetGreeting toMsg ->
( session, Api.getGreeting session toMsg )



-- EFFECT CONSTRUCTORS

Expand Down Expand Up @@ -161,6 +179,16 @@ getGreeting toMsg =
ApiEffect (GetGreeting toMsg)


getServerConfig : Api.DataResult msg ServerConfig -> Effect msg
getServerConfig toMsg =
ApiEffect (GetServerConfig toMsg)


getServerMetrics : Api.DataResult msg Metrics -> Effect msg
getServerMetrics toMsg =
ApiEffect (GetServerMetrics toMsg)


{-| Wrap a Cmd into an Effect. This is a temporary function to aid in the transition to the effect
pattern.
-}
Expand Down
6 changes: 2 additions & 4 deletions ui/src/Page/Status.elm
@@ -1,6 +1,5 @@
module Page.Status exposing (Model, Msg, init, subscriptions, update, view)

import Api
import Data.Metrics exposing (Metrics)
import Data.ServerConfig exposing (ServerConfig)
import Data.Session exposing (Session)
Expand Down Expand Up @@ -85,7 +84,7 @@ init session =
}
, Effect.batch
[ Task.perform Tick Time.now |> Effect.wrap
, Api.getServerConfig session ServerConfigLoaded |> Effect.wrap
, Effect.getServerConfig ServerConfigLoaded
]
)

Expand Down Expand Up @@ -132,8 +131,7 @@ update msg model =

Tick time ->
( { model | now = time }
, Api.getServerMetrics model.session MetricsReceived
|> Effect.wrap
, Effect.getServerMetrics MetricsReceived
)


Expand Down

0 comments on commit c74bb6e

Please sign in to comment.