Hi, I am currently struggling with port subscriptions in a submodule
(`Part`)
reused multiple times in a bigger (`Group`) module like:
-- In Group.elm
type alias Model =
{ parts : Array Part.Model }
I've been using the following "batching" technique (here are the relevant
parts of 3 files: Part.elm, Ports.elm, Group.elm)
-- Part.elm
type Msg
= PortMsg (Some, Values)
update msg model =
case msg of
PortMsg (some, values) -> (...) -- using Debug.log here to see how many
times this is called
subscriptions : Model -> Sub Msg
subscriptions model =
Ports.portFunction PortMsg
-- Ports.elm
port portFunction : ((Some, Values) -> msg) -> Sub msg
-- Group.elm
type alias Model =
{ parts : Array Part.Model }
type Msg
= PartMsg Int Part.Msg
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
<| Array.toList <| Array.indexedMap (Sub.map << PartMsg) (Array.map
Part.subscriptions
model.parts)
The problem I face is that when a Sub Msg is processed, the updates of all
the parts are triggered instead of just the only one
that should be subscribing to the msg (I verified it with the `Debug.log`
mentionned in the code).
This means that if I have 10 parts in the group, it will trigger the update
function of the right part and also of the 9 other parts that should not be
updated with this sub msg.
This is a huge efficiency problem in addition to potentially modifying my
models incorrectly if I don't care somehow in the update function.
I fear I have come to this issue by design flaws since I'm not finding a
way to overcome it.
Does anyone have an idea of what I'm doing wrong ?
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.