AshPhoenix.Form.submit is triggering change function to run twice
Say I have a custom change module, like so:
When I run this in the
iex terminal, the dbg statement only gets output once. That's what I would expect.
Now, when I use it in form using AshPhoenix.Form, I get the dbg statement two times when doing Form.submit. I even took out all other code so this was the only thing running. The mount would look like this:
Inside the my_action action, it would look like this:
Then the event handler looks like this:
With all this in place, the dbg message at the start ("my cool change") ends up being called two times. I can put a dbg in the handle_event and it's only called once. I've checked everywhere along the path and it's during the Form.submit call that it happens two times. Is that expected behavior or a bug?
If I debug inside something like a before_action that is defined in that same change module, the before_action debug only gets called once, same with action_action. But any code inside that change function at the root gets run two times.7 Replies
This is by design 😄
the body of changes are meant to be for cheap setup that can run validations on keypress
and hooks are designed to schedule logic for when the action is actually run
Ah, so anything else should be done in those hooks like
before_action and after_action then?Yep! Any side effects, or especially slow work etc.
I was going crazy trying to figure out what was going on. It took a while to track it down hahah. I assume even though that
change function is run twice, it doesn't seem to queue the hooks more than once, right?Hexdocs Search Results
Searched
ash-3.5.21 for multi-stepash-3.5.21 | extras
Correct
The form discards and rebuilds the underlying changeset each time
So its a fresh changeset w/ no hooks each time, and so hooks don't get doubled up etc.
That guide I linked has some useful info on hooks, how they work and why etc.
All right, I feel much better now knowing this. Thanks very much!