Chaining async commands

seansutton's Avatar

seansutton

26 Jun, 2013 12:49 PM

Hi,

I am hoping one of you good people could help me with chaining async commands in RL1, which need to be executed in order.

I have a series of commands that take advantage of the detain() and release() methods or RL commands. These are each asynchronous commands that need to wait for certain activities to complete before dispatching 'completed' events for other actors to react to. Generally - I am waiting for services to update remote Databases.

Now - each of these commands are usable in their own right, and I'd like to keep them that way. But sometimes I'd like for two or more async commands to be chained together.

So, the flow works something like this (step 4 is what I'd really like an answer to):

1. event is dispatched and mapped to ChainCommand. ChainCommand executes.
2. ChainCommand immediately gets detained.
3. ChainCommand sends event to execute async CommandA
4. When CommandA is released, ChainCommand is (somehow) informed.
5. ChainCommand then sends an event to execute async CommandB
4. When CommandB is released, ChainCommand is again informed.
5. ChainCommand is released.

Is this possible? hope this approach makes sense.

Many thanks!

  1. Support Staff 1 Posted by creynders on 26 Jun, 2013 01:02 PM

    creynders's Avatar

    Daisy chaining commands gets real messy really fast. I'd definitely recommend against it.
    There's a very handy RL1 utility I use to sequence commands (synced, asynced or mixed) called macrobot
    https://github.com/Aaronius/robotlegs-utilities-Macrobot

    Or the other option is to use a statemachine, which is the cleanest, since it allows you to create far more complex behaviours influenced by the results of each command:
    https://github.com/joelhooks/robotlegs-utilities-StateMachine

  2. 2 Posted by seansutton on 26 Jun, 2013 01:46 PM

    seansutton's Avatar

    Thanks for your reply creynders :)

    I think that with every application I have built with PureMVC or Robotlegs, I have reached a stage where 'daisy-chaining' some commands effectively becomes essential for development to continue. Why do you think this invariably becomes 'messy'?

    If - as in my above example - CommandA and CommandB are functional in their own right, and correctly loosely coupled with the other actors, why are a few higher level commands out of the question? The fact that Robotlegs has detain/release and PureMVC has macro commands implies there's a logic to what I'm saying.

    Thank you for the links! I have found the barrier to entry with the Statemachine design pattern and as3 mvc frameworks very high, though. I have found very few examples over the years to help with my own implementation, and it strikes me that a lot of events would get fired with this pattern that would frequently get ignored, and present worryingly misleading code.

    Procedurally executing and observing controller code is always a vital part of any project I work on. Async management with as3 is hard enough and I've always felt RL could make a great difference here (on top of all the other awesome stuff it already does!).

    I really welcome further discussion here while I check out macrobot!

    Thanks again creynders!

  3. Support Staff 3 Posted by creynders on 26 Jun, 2013 02:20 PM

    creynders's Avatar

    No, no, having commands executed in some kind of sequence is a very common
    requirement. _Daisy chaining_ commands however is a bad idea. For instance:
    CommandA calls CommandB when finished, CommandB calls CommandC when
    finished etc. will get you in the innermost circle of Hell in no time.

    State machines take a little while getting used to, but they're very
    powerful and once you "get" them you'll be wondering what was so hard about
    them. And also, you'll be using them all the time.
    They are very well worth the effort.

  4. 4 Posted by seansutton on 26 Jun, 2013 02:29 PM

    seansutton's Avatar

    Ah thanks creynders. I see that my use of the word 'chaining' was pretty misleading in my 1st post in particular. Thank you for sticking with me.

    Do you have any good resources / examples for RL1 statemachine implementation?

    Thanks again!

  5. seansutton closed this discussion on 27 Jun, 2013 08:04 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac