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
, subscriptions = \_ -> Sub.none
, subscriptions = Sub.none
}
|> RssPlugin.generate
{ siteTagline = Site.tagline

View File

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

View File

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

View File

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

View File

@ -206,13 +206,42 @@ type alias SiteConfig =
, 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 } =
Pages.Platform.init
{ init = init Nothing
, view = view
, update = update
, subscriptions = subscriptions
, subscriptions =
\\metadata path model ->
Sub.batch
[ subscriptions
, templateSubscriptions metadata path model
]
, documents = documents
, onPageChange = Just OnPageChange
, manifest = site.manifest

View File

@ -727,7 +727,7 @@ application :
}
-> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg
, subscriptions : metadata -> PagePath pathKey -> userModel -> Sub userMsg
, view :
List ( PagePath pathKey, metadata )
->
@ -822,10 +822,33 @@ application config =
\outerModel ->
case outerModel of
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
[ config.subscriptions model.userModel
|> Sub.map UserMsg
|> Sub.map AppMsg
[ userSub
, config.fromJsPort
|> Sub.map
(\decodeValue ->
@ -863,7 +886,7 @@ cliApplication :
}
-> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg
, subscriptions : metadata -> PagePath pathKey -> userModel -> Sub userMsg
, view :
List ( PagePath pathKey, metadata )
->

View File

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

View File

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