Skip to content

Dependency Mapping

Eric McGary edited this page Jan 6, 2013 · 3 revisions

There may be times when you have a task group that rather than fault or cancel the whole group, if a sub task fails you simply want to skip it and move onto the next in the queue. You may also want to then prevent any tasks that may have been dependednt on this task from executing. This is all possible and built into the MonkeyBars library. We'll look at this in the followin example...
	var Custom = MonkeyBars.Task.extend({
		performTask:function(){
			if(this.state === MonkeyBars.TaskStates.Canceled) return;
			this.complete();
		}
	});

	var t1 = new Custom({ 
		name:"one", 
		performTask:function(){ 
			this.cancel(); 
		} 
	});

	var sequence = new MonkeyBars.SequenceTask({
		loggingEnabled:false,
		tasks:[
			t1,
			new Custom({ 
				name:"two_name"
			}),
			new Custom({ 
				id:"three", 
				name:"three", 
				dependencies:[t1] 
			}),
			new Custom({ 
				id:"four_id", 
				name:"four" 
			}),
			{ 
				id:"five", 
				name:"five", 
				dependencies:["three"] 
			},
			new Custom({ 
				id:"six", 
				name:"six", 
				dependencies:["five"] 
			})
		]
	});

	sequence.start();

Im illustrating a few things here. One, that dependencies can be reference in three ways.

  1. By the task instance itself
  • By the task's user defined name
  • By the task's user defined id

Two that once a task has been canceled all other group sub tasks who are dependent on that task have their state set to canceled, which prevents them from executing and are thus skipped.

And three that the group task sequence still completes even though some of it's sub tasks have been canceled.

An important thing to note is that when a sub task calls fault it still causes its group to fault. The dependency map is in place so that you can replace calling fault, which will fault the group, and instead call cancel and continue on with subsequent sub tasks.

So the previous example was showing how the dependency map works with a SequenceTask type. It can also work with a ParallelTask type. We'll look at this in the following example...

	var t1 = new MonkeyBars.Task({
		name:"t1",
		performTask:function(){
			//
		}
	});

	var t2 = new MonkeyBars.Task({
		name:"t2",
		dependencies:[t1],
		performTask:function() { 
			this.complete(); 
		}
	});

	var t3 = new MonkeyBars.Task({
		name:"t3",
		dependencies:[t2],
		performTask:function(){ 
			this.complete(); 
		}
	});

	var group = new MonkeyBars.ParallelTask({
		tasks:[t1,t2,t3]
	});

	group.start();
	t1.complete();

This is a contrived example, but it adequately illustrates that tasks 2 and 3 do not execute until task 1 completes.

Clone this wiki locally