yukatan / commangular

Command framework for AngularJS
MIT License
83 stars 8 forks source link

Dispatching events from another event #6

Closed thylo closed 9 years ago

thylo commented 9 years ago

Hi,

Just a quick question about nesting events. Let say I have two events running a sequence of commands.

$commangularProvider.mapTo('EventA')
                .asSequence()
                .add('Command1')
                .add('Command2')
                .add('Command3')
            ;
$commangularProvider.mapTo('EventB')
                .asSequence()
                .add('Command4')
                .add('Command5')
                .add('Command6')
            ;

Now i'd like to be able to wait for this "EventA" queue to dispatch the "EventB" queue. I could merge all 6 commands in "EventC" but then I would have to maintain extra code if I want to keep consistency with A or B behaviors.

How would you deal with this ?

$commangularProvider.mapTo('EventC')
                .asSequence()
                .add('CommandDispatchingEventA') //waiting for Event A queue to complete
                .add('CommandDispatchingEventB') //waiting for Event B queue to complete
            ;
yukatan commented 9 years ago

I think you have multiple options to do that.

You can use the promise returned by the dispatcher

$commangular.dispatch('EventA').then(function(){
   $commangular.dispatch('EventB')
);

You can even pass the result from the eventA to the EventB as data.

Another option would be to use a @After event aspect

commangular.eventAspect('@After(/.*EventA/)',['$commangular',function($commangular) {

  return {

        execute: function() {
                   $commangular.dispatch('EventB');  
        }
      }
});

The aspect will allow you to have the code decoupled from EventA and EventB logic.

If the execution of EventB depends on some conditions you can check it in the aspect logic or you can then use command flows.

thylo commented 9 years ago

Thanks for the answer, I'll look at those more deeply.