How to send Msg to Shared in elm-spa?

This must be really obvious, but I’m just missing it

On elm-spa we have the “Msg type as the only way to update Shared.Model.”

How does code in, for example, Home_.elm send a Shared.Msg so it can update the shared model?

2 Likes

Is it Cmd.map?

I’m trying to use this but when I specify Cmd.map Shared.Msg.SetBirthdays newBirthdays I’m getting No definition found for Shared.Msg.SetBirthdays

In Shared.elm:

type Msg
    = SetBirthdays (List PlanetaryBirthday)

This page has a code example with something called Effect Msg and Effect.fromShared: elm-spa

I’ve never used elm-spa though so I don’t know what those are. Maybe this helps anyway!

Thanks for the suggestion.
Unfortunately, I’m not using Page.advance' type pages where Effect` is implemented for.

I think you need Page.advanced for sending shared Messages from a page. From the docs:

Some Elm users prefer sending global updates directly from their pages, so we’ve included this Page.advanced page type.

1 Like

Thanks; I’ll give that a go

I realise you are using elm-spa and this is the way it does things but…

This pattern of requiring a message to a shared module to update the shared state seems so unnecessarily byzantine to me. Why not just have the update functions return a tuple, if shared state updates are needed?

module MyPage exposing (..)

import Shared

type alias Model = 
    (Shared.Model, LocalModel)

type alias LocalModel = 
    { ... }

update : Msg -> Model -> (Model, Cmd Msg)
update = ...

Now the update to shared state can be accomplished just by calling this update function, and unpacking the local and shared models from the tuple returned (or just make the update type return a triple update : Msg -> (Model, Shared.Model) -> (Model, Shared.Model, Cmd Msg))

1 Like

And Shared.Msg.SetBirthdays should be Shared.SetBirthdays. That is probably the reason why you get a “No definition found …” and not a type error.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.