-2

n00b. I came across this SO question about sequencing messages, how to chain Cmd Msgs , and Chad Gilbert's answer.

Kewl. Now what if the sequenced messages themselves produced commands that needed to be run before the remainder of the originally-sequenced messages? I figure that since the fold in the answer above machine-guns its way through its list, I'd switch to plain ol' recursion over a "queue" of commands to run.

That seems to work (but pls see question 2 below). Now what if one of the commands in this path needs to wait for an event/message before the remainder of the originally-sequenced messages are processed?

In the following Ellie, adapted from Chad's answer above:

https://ellie-app.com/WfGhcDFjxxa1/0

-- from the start-state ...
-- click "Click me"; this sequences messages [One, Two, Three]
-- message One generates an interim command yielding message One_A
-- which yields the log: "initializing, One, One_A, Two, Three"

Next:

-- click Reset
-- check "use port"
-- click "Click me"; sequences messages [One, Two, Three]
-- message One generates an interim command sent to an outgoing port, which 500ms later sends in something via a subscribing port
-- this yields the log: "initializing, One, Two, Three, One_AReceived "foo""
-- (of course, this is unsurprising since the outgoing port command and incoming subscription message have no relationship to each other)

Now questions:

1) an outgoing port command w/o payload is expressed as, say, port name : () -> Cmd msg. Is there an analog for incoming ports/subscriptions, i.e. receive a message w/o a payload? port name : () -> Sub msg doesn't work. (Yes, I could discard the payload, but pls note that's not the question.)

2̶)̶ ̶i̶n̶ ̶C̶h̶a̶i̶n̶M̶s̶g̶s̶,̶ ̶l̶i̶n̶e̶ ̶4̶8̶,̶ ̶I̶ ̶e̶x̶p̶e̶c̶t̶e̶d̶ ̶p̶l̶a̶c̶i̶n̶g̶ ̶t̶h̶e̶ ̶i̶n̶t̶e̶r̶i̶m̶-̶g̶e̶n̶e̶r̶a̶t̶e̶d̶ ̶c̶o̶m̶m̶a̶n̶d̶ ̶a̶t̶ ̶t̶h̶e̶ ̶h̶e̶a̶d̶/̶l̶e̶f̶t̶ ̶o̶f̶ ̶t̶h̶e̶ ̶̶!̶ ̶[̶.̶.̶.̶]̶̶ ̶c̶o̶m̶m̶a̶n̶d̶-̶l̶i̶s̶t̶ ̶w̶o̶u̶l̶d̶ ̶h̶a̶v̶e̶ ̶i̶t̶ ̶p̶r̶o̶c̶e̶s̶s̶e̶d̶ ̶n̶e̶x̶t̶.̶ ̶T̶u̶r̶n̶s̶ ̶o̶u̶t̶ ̶t̶h̶a̶t̶'̶s̶ ̶n̶o̶t̶ ̶t̶h̶e̶ ̶c̶a̶s̶e̶;̶ ̶i̶t̶ ̶n̶e̶e̶d̶s̶ ̶t̶o̶ ̶a̶p̶p̶e̶a̶r̶ ̶a̶t̶ ̶t̶h̶e̶ ̶e̶n̶d̶.̶ ̶O̶K̶ ̶.̶.̶.̶ ̶b̶u̶t̶ ̶p̶u̶t̶t̶i̶n̶g̶ ̶i̶t̶ ̶a̶t̶ ̶t̶h̶e̶ ̶h̶e̶a̶d̶,̶ ̶i̶.̶e̶.̶ ̶̶m̶o̶d̶e̶l̶2̶ ̶!̶ ̶[̶ ̶c̶m̶d̶s̶1̶,̶ ̶c̶r̶e̶a̶t̶e̶C̶m̶d̶ ̶(̶C̶h̶a̶i̶n̶M̶s̶g̶s̶ ̶y̶s̶)̶ ̶]̶̶ ̶(̶n̶o̶ ̶p̶o̶r̶t̶s̶)̶ ̶y̶i̶e̶l̶d̶s̶ ̶"̶i̶n̶i̶t̶i̶a̶l̶i̶z̶i̶n̶g̶,̶ ̶O̶n̶e̶,̶ ̶T̶w̶o̶,̶ ̶̶̶O̶n̶e̶_̶A̶̶̶,̶ ̶T̶h̶r̶e̶e̶"̶.̶ ̶W̶h̶y̶?̶ ̶I̶s̶ ̶t̶h̶e̶ ̶p̶r̶o̶c̶e̶s̶s̶i̶n̶g̶ ̶o̶r̶d̶e̶r̶ ̶o̶f̶ ̶b̶a̶t̶c̶h̶e̶d̶ ̶c̶o̶m̶m̶a̶n̶d̶s̶ ̶n̶o̶t̶ ̶f̶i̶x̶e̶d̶?̶

ANSWER to 2:

I'd assumed from the List in Platform.Cmd batch : List (Cmd msg) -> Cmd msg that batch could imply ordering to commands. From continued reading elsewhere, apparently not: e.g. see "xarvh Mar 23, 2017 01:05" here. To paraphrase excerpt of response:

Task is for chaining [and order-of-execution, Ed.]. Cmd is for telling the runtime "I have a chunk of work for you"

3) and now the question that drove all this: is there an Elm "pattern" (à la wait-loops, continuations, etc in other languages) whereby one sequences/chains messages, and in their processing waits for specific events before continuing?

Community
  • 1
  • 1
pakx
  • 236
  • 1
  • 6
  • 1
    No offense, but can you re-write this more succinctly? A stackoverflow question should contain only a single question, and be straight to the point. – Emmanuel Rosa Apr 18 '17 at 21:52
  • Emmanuel, none taken, and thanx for the advice. Chalk it up to my new-ness to SO, as well as my thought to provide motivating context for the main question asked -- which is question 3 at the end. I've since come across proposals/discussions of "port tasks" which would apply here but, alas, isn't yet a "thing". – pakx Apr 19 '17 at 00:38

1 Answers1

1

Here's what I've found since:

1) the analog to outgoing port name: () -> Cmd msg is incoming port name: (() -> msg) -> Sub msg (Thx to ilias on the Slack channel)

2) Commands don't have implied order-of-execution

3) Tasks, where usable, for chainability and order of execution. Best I've found, if an interdependent sequence-of-messages to be processed yield an arbitrary mix of more intermediate messages/tasks/port-communications, etc, that themselves need to be processed before continuing w/ that original sequence-of-messages there isn't "one ring to chain them all" -- rethink the design.

pakx
  • 236
  • 1
  • 6