question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"SetPage" duplicated when dispatched through Cmd.ofMsg

See original GitHub issue

When the router is connected to the Elmish state using Router.infer, routing commands issued through Cmd.ofMsg are duplicated.

Here is a minimal example (see github):

type Page =
    | [<EndPoint "/">] Home
    | [<EndPoint "/secondPage">] NewPage    
type Message =
    | SetPage of Page
    | DoAThing
let update message model =
   // This demonstrates which messages are duplicated  
    Console.WriteLine($"{message}")
    match message with
    | SetPage page ->
        {model with page = page}, Cmd.none
    | DoAThing ->
        model, Cmd.ofMsg (SetPage NewPage)
let router = Router.infer SetPage (fun model -> model.page)

When DoAThing is dispatched from Home, SetPage NewPage gets dispatched twice. This causes unexpected behavior whenever one wants to trigger a redirect after performing an action, i.e. create a new resource and then navigate to the corresponding view.

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Tarmilcommented, May 13, 2023

Yeah, we can’t really detect that a URL change was caused by the same message that it triggers. So it’s best to avoid dispatching it directly. Instead you can either have a separate message like you did, or directly set { model with page = page } in the update for DoAThing.

Maybe the documentation should recommend calling this message something like PageChanged, to emphasize that it’s not meant to be dispatched directly.

1reaction
Martin521commented, May 11, 2023

I have not used routing myself, but as far as I understand it, the SetPage message is indeed not meant to be used by you, but only by the router. It is only a way to flexibly configure the router to manipulate the model in the right way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"SetPage" duplicated when dispatched through Cmd.ofMsg
When the router is connected to the Elmish state using Router.infer , routing commands issued through Cmd.ofMsg are duplicated.
Read more >
My tips for working with Elmish
Use Cmd. performFunc to evaluate a simple function and map the success to a message discarding any possible error.
Read more >
Page
Page provides methods to interact with a single tab in a Browser, or an extension background page in Chromium. One Browser instance might...
Read more >
Cmd.ofMsg is gone. Why? · Issue #170 · elmish ...
The message to message to send when the task finish with success. While Cmd.OfFunc.result just takes a message and dispatch it back. I...
Read more >
ACUCOBOL-GT
ACUCOBOL-GT supports the emulation of graphical controls and windows on character-based systems. This emulation allows you to more easily write.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found