mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-24 15:12:01 +03:00
89 lines
1.8 KiB
Elm
89 lines
1.8 KiB
Elm
module Effect exposing (Effect(..), batch, fromCmd, map, none, perform)
|
|
|
|
import Browser.Navigation
|
|
import Http
|
|
import Url exposing (Url)
|
|
|
|
|
|
type Effect msg
|
|
= None
|
|
| Cmd (Cmd msg)
|
|
| Batch (List (Effect msg))
|
|
| FetchPageData
|
|
{ body : Maybe { contentType : String, body : String }
|
|
, path : Maybe String
|
|
, toMsg : Result Http.Error Url -> msg
|
|
}
|
|
|
|
|
|
type alias RequestInfo =
|
|
{ contentType : String
|
|
, body : String
|
|
}
|
|
|
|
|
|
none : Effect msg
|
|
none =
|
|
None
|
|
|
|
|
|
batch : List (Effect msg) -> Effect msg
|
|
batch =
|
|
Batch
|
|
|
|
|
|
fromCmd : Cmd msg -> Effect msg
|
|
fromCmd =
|
|
Cmd
|
|
|
|
|
|
map : (a -> b) -> Effect a -> Effect b
|
|
map fn effect =
|
|
case effect of
|
|
None ->
|
|
None
|
|
|
|
Cmd cmd ->
|
|
Cmd (Cmd.map fn cmd)
|
|
|
|
Batch list ->
|
|
Batch (List.map (map fn) list)
|
|
|
|
FetchPageData fetchInfo ->
|
|
FetchPageData
|
|
{ body = fetchInfo.body
|
|
, path = fetchInfo.path
|
|
, toMsg = fetchInfo.toMsg >> fn
|
|
}
|
|
|
|
|
|
perform :
|
|
{ fetchRouteData :
|
|
{ body : Maybe { contentType : String, body : String }
|
|
, path : Maybe String
|
|
, toMsg : Result Http.Error Url -> pageMsg
|
|
}
|
|
-> Cmd msg
|
|
}
|
|
-> (pageMsg -> msg)
|
|
-> Browser.Navigation.Key
|
|
-> Effect pageMsg
|
|
-> Cmd msg
|
|
perform info fromPageMsg key effect =
|
|
case effect of
|
|
None ->
|
|
Cmd.none
|
|
|
|
Cmd cmd ->
|
|
Cmd.map fromPageMsg cmd
|
|
|
|
Batch list ->
|
|
Cmd.batch (List.map (perform info fromPageMsg key) list)
|
|
|
|
FetchPageData fetchInfo ->
|
|
info.fetchRouteData
|
|
{ body = fetchInfo.body
|
|
, path = fetchInfo.path
|
|
, toMsg = fetchInfo.toMsg
|
|
}
|