Skip to content

Commit

Permalink
operation queue will remove an operation when the operation fires a
Browse files Browse the repository at this point in the history
canceled event. [#29]
  • Loading branch information
danschultz committed Mar 8, 2011
1 parent 99834b0 commit 35812c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
24 changes: 20 additions & 4 deletions src/operations/Operation.as
@@ -1,6 +1,5 @@
package operations
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.getQualifiedClassName;

Expand All @@ -11,6 +10,11 @@ package operations
*/
[Event(name="canceled", type="operations.OperationEvent")]

/**
* Dispatched when the execution of an operation has been queued.
*/
[Event(name="queued", type="operations.OperationEvent")]

/**
* Dispatched after the execution of an operation has started.
*/
Expand Down Expand Up @@ -200,6 +204,13 @@ package operations
}
}

private function fireQueued():void
{
if (hasEventListener(OperationEvent.QUEUED)) {
dispatchEvent( new OperationEvent(OperationEvent.QUEUED) );
}
}

private function fireAfterExecute():void
{
if (hasEventListener(OperationEvent.AFTER_EXECUTE)) {
Expand Down Expand Up @@ -271,14 +282,19 @@ package operations
* canceled. If the operation has finished its errors, result data, and progress will
* be reset.
*/
final public function reset():void
final public function queue():void
{
if (isExecuting) {
cancel();
cancelRequest();
}

progress.complete = 0;
_hasErrored = false;
changeState(QUEUED);

if (!isQueued) {
changeState(QUEUED);
fireQueued();
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/operations/OperationEvent.as
Expand Up @@ -16,6 +16,11 @@ package operations
*/
public static const CANCELED:String = "canceled";

/**
* The event type for when an operation is queued.
*/
public static const QUEUED:String = "queued";

/**
* The event type before an operation is executed.
*/
Expand Down
15 changes: 12 additions & 3 deletions src/operations/OperationQueue.as
@@ -1,5 +1,6 @@
package operations
{
import flash.events.Event;
import flash.events.EventDispatcher;

import mx.collections.ArrayList;
Expand Down Expand Up @@ -55,11 +56,12 @@ package operations
public function queue(operation:Operation):void
{
remove(operation);
operation.reset();
operation.queue();
items.addItem(operation);
progress.total += operation.progress.total;

executeAvailable();

operation.addEventListener(OperationEvent.CANCELED, handleOperationCanceled);
}

/**
Expand All @@ -71,6 +73,8 @@ package operations
public function remove(operation:Operation):void
{
if (_items.removeItem(operation)) {
operation.removeEventListener(OperationEvent.CANCELED, handleOperationCanceled);

if (operation.isFinished) {
progress.complete -= operation.progress.complete;
}
Expand Down Expand Up @@ -109,7 +113,7 @@ package operations
_isRunning = false;

while (executing.length > 0) {
(executing.getItemAt(0) as Operation).reset();
(executing.getItemAt(0) as Operation).queue();
}
}
}
Expand All @@ -132,6 +136,11 @@ package operations
operation.execute();
}

private function handleOperationCanceled(event:OperationEvent):void
{
remove(event.operation);
}

private function handleOperationProgress(event:ProgressOperationEvent):void
{

Expand Down

0 comments on commit 35812c7

Please sign in to comment.