Agile software development using Kanban & Scrum. We code Flex & Ruby on Rails in Auckland, New Zealand.

  • Robotlegs Bootstrap Actor

    I’m sharing the following Bootstrap Actor class, which simplifies bootstrap sequencing in Robotlegs.

    I figure it has some value because Stray, who has a fairly similar programming ethos to me, see’s some merit in it.

    Usage first.

    Trigger the bootstrap in your context

    override public function startup():void {
        commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE, BootstrapCommand, ContextEvent);
        super.startup();
    }
    

    Which calls the BootstrapCommand

    import org.robotlegs.mvcs.Command;
    
    public class BootstrapCommand extends Command {
    
        override public function execute():void {
            injector.mapSingleton(ConfigurationSequence);
            injector.getInstance(ConfigurationSequence).step();
        }
    
    }
    

    Then implement the sequence

    public class ConfigurationSequence extends AbstractSequencer {
    
        override protected function configure():void {
            addStep(ConfigureWorkersModelCommand);
            addStep(ConfigureScalesModelCommand);
    
            ... yadda yadda ... 
    
            addStep(ConfigureJobDraggingModel);
        }
    
    }
    

    What happens is that the line in the bootstrap

    injector.getInstance(ConfigurationSequence).step();
    

    instances the Sequencer and tells it to run the next step, which in this case, is the first one.

    Each command added with ‘addStep’ is added as a command mapping which responds to a SequenceStepEvent with a named type. This is not conventional behavior for an event, but it works really well in this context.

    Finally, a sequenced command looks a bit like this:

    public class ConfigureScheduleHeaderViewCommand extends Command {
    
        [Inject]
        public var event:SequenceStepEvent;
    
        override public function execute():void {
            mediatorMap.mapView(TimelineView, TimelineMediator);
            ... yadda yadda ...
    
            event.step();
        }
    }
    

    The event.step causes the next item to be run.

    Source Code

    The sequencer gets the addStep() and configure() methods from AbstractSequencer.as.

    You also need the SequenceStepEvent.as class

    Options

    Bootstrap command and context

    You could merge BootstrapCommand into the context. I prefer to overdo SRP than underdo it.

    Automatically step

    Instead of specifying event.step() in each sequence command, you could modify AbstractSequencer#executeNextStep like this:

    private function executeNextStep():void {
        var classFQN:String = steps.shift();
        var event:SequenceStepEvent = new SequenceStepEvent(classFQN, this);
    
        dispatch(event);
    
        step();
    }
    
    1. vworkdev posted this