Wrapping a message in Cmd

1,217 views
Skip to first unread message

Luke Williams

unread,
Jun 16, 2016, 1:34:52 PM6/16/16
to Elm Discuss
Hi, 

Please let me know if there's already a good way of doing this! :)

I've been writing Elm for a long time but stepped away for a couple of months and a bunch has changed! Now there are commands instead of signals, Html is parameterised by message type and I'll be honest with you: I'm loving it. There is but one function I wish were provided: to wrap a message in a Cmd and have it piped back in at the top of my program. 

This is never necessary, of course. If I want to return a list of action-triggering commands as well as simple messages, I could obviously just return two lists. It would ultimately be up to the caller to see that they are distributed. There's nothing wrong with this, but it's kind of messy. I want to be able to write components with event handlers that feel like the Html libraries, but with events generated from within Elm, rather than by external triggers. The events are emitted via the component's update function.

The example in front of me right now is a looping progress bar: we feed it clock ticks, and at the end of every cycle it wants to fire an event. There's no corresponding external event because the looping logic is all in Elm, but it would be nice not to have implement separate paths for events that are coming from inside vs. outside. From the component user's perspective, they're all just events. We can do this by making a Task that always succeeds and `perform`ing it, but who wants to do that all the time? 

I call my function to do this `return`, and I've put it into a new library called `cmd-extra`, because I notice there isn't an existing one listed yet: https://github.com/shmookey/cmd-extra

Thoughts / feelings / suggestions? 

Cheers,
Luke

Janis Voigtländer

unread,
Jun 16, 2016, 1:54:53 PM6/16/16
to elm-d...@googlegroups.com

It’s always a good idea to have a look at the FAQ first.

https://elm-community.github.io/elm-faq/17.html#how-do-i-generate-a-new-message-as-a-command


--
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 elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luke Williams

unread,
Jun 17, 2016, 12:01:33 AM6/17/16
to Elm Discuss

Ah yes thank you for that, it is indeed good to know that I have not missed anything substantive with regards to the two possible approaches to propagating generated messages.


The `update-extra` library helps make the case I described as "messy" a little less so, but that approach has other issues - e.g. internally-generated messages are always handled before other pending messages, potentially resulting in an infinite loop or stack overflow, depending on implementation.


Do you have any thoughts about encapsulating the alternative approach, also described in the FAQ entry, as a function in a new `cmd-extra` library as I have done?


Cheers,

Luke

Janis Voigtländer

unread,
Jun 17, 2016, 1:26:40 AM6/17/16
to elm-d...@googlegroups.com

Do you have any thoughts about encapsulating the alternative approach, also described in the FAQ entry, as a function in a new cmd-extra library as I have done?

I think it’s a fine thing to do. But I think succeed would be a better name than return. And that each of the definitions from the FAQ page would be a better choice for the implementation.

Luke Williams

unread,
Jun 17, 2016, 2:13:00 AM6/17/16
to elm-d...@googlegroups.com
I considered `succeed`, but I think it's a bit misleading since there is no corresponding notion of failure.

I appreciate you taking a look out the implementation! I'll take another look this evening.
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/_qDpS2wxccE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.

Janis Voigtländer

unread,
Jun 17, 2016, 2:30:20 AM6/17/16
to elm-d...@googlegroups.com

I considered succeed, but I think it’s a bit misleading since there is no corresponding notion of failure.

Just message : msg -> Cmd msg, then?

Luke Williams

unread,
Jun 18, 2016, 9:35:31 AM6/18/16
to Elm Discuss
Aha, yes, I like that. The term `return` would have been likely to confuse newcomers from JS. 

I think this tiny library is ready for public consumption. I've published it and it's now available for general use:


Thanks again for your help!
Reply all
Reply to author
Forward
0 new messages