Skip to content

Instantly share code, notes, and snippets.

@vrobel
Created September 15, 2011 14:12
Show Gist options
  • Save vrobel/1219335 to your computer and use it in GitHub Desktop.
Save vrobel/1219335 to your computer and use it in GitHub Desktop.
SequenceManager and example
package com.blackmoondev.sequence.example {
import com.blackmoondev.sequence.example.services.InitLoaderService;
import com.blackmoondev.sequence.example.services.core.IInitLoaderService;
import com.blackmoondev.sequence.example.signals.LoadIconsSignal;
import com.blackmoondev.sequence.example.signals.LoadMainConfigSignal;
import com.blackmoondev.sequence.example.signals.LoadUserConfigSignal;
import com.blackmoondev.sequence.example.signals.initloader.InitFlashLibSignal;
import com.blackmoondev.sequence.SequenceManager;
public class ExampleSequenceManager extends SequenceManager {
/**
* SEQUENCE COMMANDS
*/
[Inject]
public var loadMainConfig:LoadMainConfigSignal;
[Inject]
public var loadUserConfig:LoadUserConfigSignal;
[Inject]
public var initFlashLib:InitFlashLibSignal;
[Inject]
public var loadIcons:LoadIconsSignal;
/**
* INJECTS
*/
[Inject]
public var config:ConfigModel;
override protected function setSteps():void {
addStep(loadMainConfig, [config.configUrl]);
addStep(loadUserConfig); //Param if null will be get from getLiveParams
addStep(initFlashLib, []);
addStep(loadIcons,[]);
}
override protected function getLiveParams(step:*):Array {
switch(step) {
case loadUserConfig:
return [config.username];
default:
return null;
}
}
}
}
package com.blackmoondev.sequence {
import com.blackmoondev.sequence.signals.*;
import flash.utils.Dictionary;
import org.osflash.signals.IDispatcher;
/* PASTE IT TO YOUR APP CONTEXT
signalCommandMap.mapSignalClass(com.blackmoondev.sequence.signals.SequenceFinishedSignal, SequenceFinishedCommand);
signalCommandMap.mapSignalClass(com.blackmoondev.sequence.signals.SequenceFailedSignal, SequenceFailedCommand);
injector.mapSingleton(com.blackmoondev.sequence.signals.SequenceStepFailedSignal);
injector.mapSingleton(com.blackmoondev.sequence.signals.SequenceStepFinishedSignal);
*/
public class SequenceManager {
[Inject]
public var sequenceFinished:SequenceFinishedSignal;
[Inject]
public var sequenceFailed:SequenceFailedSignal;
[Inject]
public var sequenceStepFinished:SequenceStepFinishedSignal;
[Inject]
public var sequenceStepFailed:SequenceStepFailedSignal;
protected var sequenceSteps:Array = [];
protected var sequenceStepParams:Dictionary = new Dictionary();
/**
* METHODS
*/
public function addStep(step:*, params:Array = null):void {
sequenceSteps.push(step);
if(params)
sequenceStepParams[step] = params;
}
public function startSequence():void {
setSteps();
setupStepListeners();
nextStep()
}
private function setupStepListeners():void {
sequenceStepFinished.add(nextStep);
sequenceStepFailed.add(failSequence);
}
private function removeListeners():void {
sequenceStepFinished.remove(nextStep);
sequenceStepFailed.remove(failSequence);
}
protected function failSequence(msg:*):void {
sequenceSteps = [];
sequenceStepFinished.remove(nextStep);
sequenceStepFailed.remove(failSequence);
sequenceFailed.dispatch(msg.toString());
onFail();
}
protected function setSteps():void {
//OVERRIDE HERE
}
private function nextStep():void {
if (sequenceSteps.length == 0) {
removeListeners();
sequenceFinished.dispatch();
onFinish()
} else {
//step is a function
var step:* = sequenceSteps.shift();
var params:Array = sequenceStepParams[step] || getLiveParams(step) || [];
if (step is IDispatcher) {
IDispatcher(step).dispatch.apply(step, params);
} else {
//step is a function
Function(step).apply(null, params)
}
onStepDispatched()
}
}
protected function onStepDispatched():void {
}
protected function onFinish():void {
}
protected function onFail():void {
}
protected function getLiveParams(step:*):Array {
//OVERRIDE HERE
return null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment