Skip to content

Commit

Permalink
adding a queue to manage the execution of operations [#29
Browse files Browse the repository at this point in the history
state:resolved]
  • Loading branch information
danschultz committed Mar 8, 2011
1 parent f15d146 commit c1b355e
Show file tree
Hide file tree
Showing 6 changed files with 508 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/operations/CompoundOperation.as
Expand Up @@ -133,6 +133,7 @@ package operations

_executingOperations.remove(operation);
_finishedOperationsCount++;
progressed(_finishedOperationsCount);

// check to see if all the operations are finished.
if (isExecuting && _finishedOperationsCount == _operations.length) {
Expand Down Expand Up @@ -198,5 +199,13 @@ package operations
{
return _operations;
}

/**
* @inheritDoc
*/
override protected function get unitsTotal():Number
{
return operationSet.length;
}
}
}
109 changes: 104 additions & 5 deletions src/operations/Operation.as
Expand Up @@ -18,6 +18,11 @@ package operations
*/
[Event(name="beforeExecute", type="operations.OperationEvent")]

/**
* Dispatched when the operation has progressed during its execution.
*/
[Event(name="progress", type="operations.ProgressOperationEvent")]

/**
* Dispatched when either an error or fault has occurred during the execution
* of an operation.
Expand Down Expand Up @@ -46,6 +51,12 @@ package operations
*/
public class Operation extends EventDispatcher
{
private static const QUEUED:int = 0;
private static const EXECUTING:int = 1;
private static const FINISHED:int = 2;

private var _state:int = QUEUED;

/**
* Constructor.
*/
Expand Down Expand Up @@ -80,8 +91,8 @@ package operations
*/
final public function execute():void
{
if (!isExecuting) {
_isExecuting = true;
if (isQueued) {
_state = EXECUTING;
fireBeforeExecute();

if (isExecuting) {
Expand Down Expand Up @@ -150,6 +161,7 @@ package operations
{
if (isExecuting) {
fireFault(summary, detail);
_hasErrored = true;
finish(false);
}
}
Expand All @@ -166,7 +178,8 @@ package operations
final protected function finish(successful:Boolean):void
{
if (isExecuting) {
_isExecuting = false;
_state = FINISHED;
progressed(unitsTotal);
fireFinished(successful);
}
}
Expand All @@ -192,6 +205,13 @@ package operations
}
}

private function fireProgress():void
{
if (hasEventListener(ProgressOperationEvent.PROGRESS)) {
dispatchEvent( new ProgressOperationEvent(ProgressOperationEvent.PROGRESS) );
}
}

private function fireFault(summary:String, detail:String = ""):void
{
if (hasEventListener(FaultOperationEvent.FAULT)) {
Expand Down Expand Up @@ -226,6 +246,32 @@ package operations
return data;
}

/**
* Called by sub-classes to indicate the progress of the operation.
*
* @param unitsComplete The number of units completed.
*/
final protected function progressed(unitsComplete:Number):void
{
_progress.complete = unitsComplete;
fireProgress();
}

/**
* Resets the operation to its queued state. If the operation is executing, it will be
* canceled. If the operation has finished its errors, result data, and progress will
* be reset.
*/
final public function reset():void
{
if (isExecuting) {
cancel();
}
progress.complete = 0;
_hasErrored = false;
_state = QUEUED;
}

/**
* Called by a sub-class to indicate that a result was received during the
* execution of the operation.
Expand Down Expand Up @@ -302,13 +348,66 @@ package operations
return classNameParts.length > 1 ? classNameParts[1] : classNameParts[0];
}

private var _isExecuting:Boolean;
/**
* Indicates whether the request is currently executing.
*/
final public function get isExecuting():Boolean
{
return _isExecuting;
return _state == EXECUTING;
}

/**
* Indicates whether the request is finished, either successfully or unsuccessfully.
*/
final public function get isFinished():Boolean
{
return _state == FINISHED;
}

/**
* Indicates whether the request is idle and ready to be executed.
*/
final public function get isQueued():Boolean
{
return _state == QUEUED;
}

/**
* Indicates whether the request has finished, and hasn't errored.
*/
final public function get isSuccessful():Boolean
{
return isFinished && !hasErrored;
}

private var _hasErrored:Boolean;
/**
* <code>true</code> if this operation errored during its execution.
*/
final public function get hasErrored():Boolean
{
return _hasErrored;
}

private var _progress:Progress;
/**
* Information about the progress of this operation.
*/
public function get progress():Progress
{
if (_progress == null) {
_progress = new Progress();
_progress.total = unitsTotal;
}
return _progress;
}

/**
* The number of units needed to complete this operation.
*/
protected function get unitsTotal():Number
{
return 1;
}
}
}

0 comments on commit c1b355e

Please sign in to comment.