Wire in subscriptions for templates.

This commit is contained in:
Dillon Kearns 2020-09-14 22:05:36 -07:00
parent 86da9973bd
commit e71e90172a
9 changed files with 76 additions and 14 deletions

View File

@ -25,7 +25,7 @@ main =
} }
] ]
, site = Site.config , site = Site.config
, subscriptions = \_ -> Sub.none , subscriptions = Sub.none
} }
|> RssPlugin.generate |> RssPlugin.generate
{ siteTagline = Site.tagline { siteTagline = Site.tagline

View File

@ -29,6 +29,7 @@ sandbox config =
, staticData = \_ -> StaticHttp.succeed () , staticData = \_ -> StaticHttp.succeed ()
, init = \_ -> ( (), Cmd.none ) , init = \_ -> ( (), Cmd.none )
, update = \_ _ _ -> ( (), Cmd.none, Shared.NoOp ) , update = \_ _ _ -> ( (), Cmd.none, Shared.NoOp )
, subscriptions = \_ _ _ -> Sub.none
} }
@ -55,9 +56,12 @@ simpler config =
, staticData = \_ -> StaticHttp.succeed () , staticData = \_ -> StaticHttp.succeed ()
, init = config.init , init = config.init
, update = \a1 b1 c1 -> config.update a1 b1 c1 |> (\( a, b ) -> ( a, b, Shared.NoOp )) , update = \a1 b1 c1 -> config.update a1 b1 c1 |> (\( a, b ) -> ( a, b, Shared.NoOp ))
, subscriptions = \_ _ _ -> Sub.none
} }
{-| Basic `staticData` (including access to Shared static data)
-}
stateless : stateless :
{ staticData : { staticData :
List ( PagePath Pages.PathKey, GlobalMetadata.Metadata ) List ( PagePath Pages.PathKey, GlobalMetadata.Metadata )
@ -81,9 +85,12 @@ stateless config =
, staticData = config.staticData , staticData = config.staticData
, init = \_ -> ( (), Cmd.none ) , init = \_ -> ( (), Cmd.none )
, update = \_ _ _ -> ( (), Cmd.none, Shared.NoOp ) , update = \_ _ _ -> ( (), Cmd.none, Shared.NoOp )
, subscriptions = \_ _ _ -> Sub.none
} }
{-| Full application (including local `Model`, `Msg`, `update`)
-}
application : application :
{ staticData : { staticData :
List ( PagePath Pages.PathKey, GlobalMetadata.Metadata ) List ( PagePath Pages.PathKey, GlobalMetadata.Metadata )
@ -99,6 +106,7 @@ application :
-> List (Head.Tag Pages.PathKey) -> List (Head.Tag Pages.PathKey)
, init : templateMetadata -> ( templateModel, Cmd templateMsg ) , init : templateMetadata -> ( templateModel, Cmd templateMsg )
, update : templateMetadata -> templateMsg -> DynamicPayload templateModel -> ( templateModel, Cmd templateMsg, Shared.SharedMsg ) , update : templateMetadata -> templateMsg -> DynamicPayload templateModel -> ( templateModel, Cmd templateMsg, Shared.SharedMsg )
, subscriptions : templateMetadata -> PagePath Pages.PathKey -> DynamicPayload templateModel -> Sub templateMsg
} }
-> Template templateMetadata templateStaticData templateModel templateMsg -> Template templateMetadata templateStaticData templateModel templateMsg
application config = application config =
@ -107,6 +115,7 @@ application config =
, staticData = config.staticData , staticData = config.staticData
, init = config.init , init = config.init
, update = config.update , update = config.update
, subscriptions = config.subscriptions
} }
@ -125,6 +134,7 @@ type alias Template templateMetadata templateStaticData templateModel templateMs
-> List (Head.Tag Pages.PathKey) -> List (Head.Tag Pages.PathKey)
, init : templateMetadata -> ( templateModel, Cmd templateMsg ) , init : templateMetadata -> ( templateModel, Cmd templateMsg )
, update : templateMetadata -> templateMsg -> DynamicPayload templateModel -> ( templateModel, Cmd templateMsg, Shared.SharedMsg ) , update : templateMetadata -> templateMsg -> DynamicPayload templateModel -> ( templateModel, Cmd templateMsg, Shared.SharedMsg )
, subscriptions : templateMetadata -> PagePath Pages.PathKey -> DynamicPayload templateModel -> Sub templateMsg
} }

View File

@ -27,6 +27,7 @@ template =
, staticData = staticData , staticData = staticData
, init = init , init = init
, update = update , update = update
, subscriptions = \_ _ _ -> Sub.none
} }

View File

@ -78,9 +78,7 @@ imageDecoder =
findMatchingImage : String -> Maybe (ImagePath Pages.PathKey) findMatchingImage : String -> Maybe (ImagePath Pages.PathKey)
findMatchingImage imageAssetPath = findMatchingImage imageAssetPath =
List.Extra.find List.Extra.find (\image -> ImagePath.toString image == imageAssetPath) Pages.allImages
(\image -> ImagePath.toString image == imageAssetPath)
Pages.allImages
view : view :

View File

@ -40,6 +40,7 @@ template =
, staticData = staticData , staticData = staticData
, init = init , init = init
, update = update , update = update
, subscriptions = \_ _ _ -> Sub.none
} }

View File

@ -206,13 +206,42 @@ type alias SiteConfig =
, manifest : Manifest.Config Pages.PathKey , manifest : Manifest.Config Pages.PathKey
} }
templateSubscriptions : Metadata -> PagePath Pages.PathKey -> Model -> Sub Msg
templateSubscriptions metadata path model =
case model.page of
${templates.map(name => `
Model${name} templateModel ->
case metadata of
M.Metadata${name} templateMetadata ->
Template.${name}.template.subscriptions
templateMetadata
path
{ model = templateModel
, sharedModel = model.global
}
|> Sub.map Msg${name}
_ ->
Sub.none
`
).join("\n ")}
NotFound ->
Sub.none
mainTemplate { documents, subscriptions, site } = mainTemplate { documents, subscriptions, site } =
Pages.Platform.init Pages.Platform.init
{ init = init Nothing { init = init Nothing
, view = view , view = view
, update = update , update = update
, subscriptions = subscriptions , subscriptions =
\\metadata path model ->
Sub.batch
[ subscriptions
, templateSubscriptions metadata path model
]
, documents = documents , documents = documents
, onPageChange = Just OnPageChange , onPageChange = Just OnPageChange
, manifest = site.manifest , manifest = site.manifest

View File

@ -727,7 +727,7 @@ application :
} }
-> ( userModel, Cmd userMsg ) -> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg ) , update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg , subscriptions : metadata -> PagePath pathKey -> userModel -> Sub userMsg
, view : , view :
List ( PagePath pathKey, metadata ) List ( PagePath pathKey, metadata )
-> ->
@ -822,10 +822,33 @@ application config =
\outerModel -> \outerModel ->
case outerModel of case outerModel of
Model model -> Model model ->
let
urls =
{ currentUrl = model.url
, baseUrl = model.baseUrl
}
( maybePagePath, maybeMetadata ) =
case ContentCache.lookupMetadata config.pathKey model.contentCache urls of
Just ( pagePath, metadata ) ->
( Just pagePath, Just metadata )
Nothing ->
( Nothing, Nothing )
userSub =
Maybe.map2
(\metadata path ->
config.subscriptions metadata path model.userModel
|> Sub.map UserMsg
|> Sub.map AppMsg
)
maybeMetadata
maybePagePath
|> Maybe.withDefault Sub.none
in
Sub.batch Sub.batch
[ config.subscriptions model.userModel [ userSub
|> Sub.map UserMsg
|> Sub.map AppMsg
, config.fromJsPort , config.fromJsPort
|> Sub.map |> Sub.map
(\decodeValue -> (\decodeValue ->
@ -863,7 +886,7 @@ cliApplication :
} }
-> ( userModel, Cmd userMsg ) -> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg ) , update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg , subscriptions : metadata -> PagePath pathKey -> userModel -> Sub userMsg
, view : , view :
List ( PagePath pathKey, metadata ) List ( PagePath pathKey, metadata )
-> ->

View File

@ -79,7 +79,7 @@ type alias Config pathKey userMsg userModel metadata view =
} }
-> ( userModel, Cmd userMsg ) -> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg ) , update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg , subscriptions : metadata -> PagePath pathKey -> userModel -> Sub userMsg
, view : , view :
List ( PagePath pathKey, metadata ) List ( PagePath pathKey, metadata )
-> ->

View File

@ -92,7 +92,7 @@ type Builder pathKey model msg metadata view
} }
-> ( model, Cmd msg ) -> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg ) , update : msg -> model -> ( model, Cmd msg )
, subscriptions : model -> Sub msg , subscriptions : metadata -> PagePath pathKey -> model -> Sub msg
, view : , view :
List ( PagePath pathKey, metadata ) List ( PagePath pathKey, metadata )
-> ->
@ -185,7 +185,7 @@ init :
{ view : model -> view -> { title : String, body : Html msg } { view : model -> view -> { title : String, body : Html msg }
, head : List (Head.Tag pathKey) , head : List (Head.Tag pathKey)
} }
, subscriptions : model -> Sub msg , subscriptions : metadata -> PagePath pathKey -> model -> Sub msg
, documents : , documents :
List List
{ extension : String { extension : String
@ -330,7 +330,7 @@ application :
} }
-> ( model, Cmd msg ) -> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg ) , update : msg -> model -> ( model, Cmd msg )
, subscriptions : model -> Sub msg , subscriptions : metadata -> PagePath pathKey -> model -> Sub msg
, view : , view :
List ( PagePath pathKey, metadata ) List ( PagePath pathKey, metadata )
-> ->