source: trunk/fl5/src/com/longtailvideo/jwplayer/controller/TaskQueue.as @ 597

Revision 597, 2.8 KB checked in by pablo, 4 years ago (diff)

Fixed bug where a playlist could load a playlistItem with a playlist URL

Line 
1package com.longtailvideo.jwplayer.controller {
2        import com.longtailvideo.jwplayer.events.PlayerEvent;
3       
4        import flash.events.ErrorEvent;
5        import flash.events.Event;
6        import flash.events.EventDispatcher;
7        import flash.utils.Dictionary;
8
9        /**
10         * Sent when the all of the tasks have completed successfully
11         *
12         * @eventType flash.events.Event.COMPLETE
13         */
14        [Event(name="complete", type = "flash.events.Event")]
15
16        /**
17         * Sent when an error occurred in one of the tasks.
18         *
19         * @eventType flash.events.ErrorEvent.ERROR
20         */
21        [Event(name="error", type = "flash.events.ErrorEvent")]
22
23        /**
24         * Runs tasks in a set order.  Dispatches an Event.COMPLETE if all tasks successfully complete, or an ErrorEvent.ERROR
25         * if one of the tasks in the queue fails.
26         * @author Pablo Schklowsky
27         */
28        public class TaskQueue extends EventDispatcher {
29               
30                private var activeTask:Function = null;
31                private var taskIndex:Number = -1;
32               
33                private var taskSuccess:Dictionary;
34                private var taskFailure:Dictionary;
35                private var taskOrder:Array;
36               
37                private var continueOnFailure:Boolean;
38                private var failureState:Boolean = false;
39               
40                public function TaskQueue(cont:Boolean=false) {
41                        taskOrder = [];
42                        taskSuccess = new Dictionary();
43                        taskFailure = new Dictionary();
44                        continueOnFailure = cont;
45                }
46               
47                public function queueTask(task:Function, success:Function=null, failure:Function=null):void {
48                        taskOrder.push(task);
49                        if (success != null) {
50                                taskSuccess[task] = success;
51                                taskFailure[task] = failure;
52                        }
53                }
54               
55                public function runTasks():void {
56                        if (activeTask == null) {
57                                nextTask();
58                        }
59                }
60
61                public function success(event:Event=null):void {
62                        if (!failureState) {
63                                var runSuccess:Function = taskSuccess[activeTask] as Function;
64                                if (runSuccess != null) {
65                                        runSuccess(event);
66                                }
67                                nextTask();
68                        }
69                }
70               
71                public function failure(event:Event):void {
72                        var runFailure:Function = taskFailure[activeTask] as Function;
73                        if (runFailure != null) {
74                                runFailure(event);
75                        }
76                       
77                        if (event is ErrorEvent) {
78                                dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false,
79                                        "Task Queue failed at step " + taskIndex + ": " +  ErrorEvent(event).text));
80                        } else if (event is PlayerEvent && (event as PlayerEvent).message) {
81                                dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false,
82                                        "Task Queue failed at step " + taskIndex + ": " +  PlayerEvent(event).message));
83                        } else {
84                                dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false,
85                                                                "Task Queue failed at step " + taskIndex + ": " +  event.toString()));
86                        }
87                       
88                        if (continueOnFailure) {
89                                nextTask();
90                        } else {
91                                failureState = true;
92                        }
93                }
94
95                private function nextTask():void {
96                        if (taskOrder.length > 0) {
97                                activeTask = taskOrder.shift() as Function;
98                                taskIndex++;
99                                activeTask();
100                        } else {
101                                dispatchEvent(new Event(Event.COMPLETE));
102                        }
103                }
104               
105        }
106}
Note: See TracBrowser for help on using the repository browser.