source: trunk/web/addons/job_monarch/lib/extjs/source/util/TaskMgr.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 5.4 KB
Line 
1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/**
10 * @class Ext.util.TaskRunner
11 * Provides the ability to execute one or more arbitrary tasks in a multithreaded manner.  Generally, you can use
12 * the singleton {@link Ext.TaskMgr} instead, but if needed, you can create separate instances of TaskRunner.  Any
13 * number of separate tasks can be started at any time and will run independently of each other.  Example usage:
14 * <pre><code>
15// Start a simple clock task that updates a div once per second
16var task = {
17    run: function(){
18        Ext.fly('clock').update(new Date().format('g:i:s A'));
19    },
20    interval: 1000 //1 second
21}
22var runner = new Ext.util.TaskRunner();
23runner.start(task);
24</code></pre>
25 * @constructor
26 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
27 * (defaults to 10)
28 */
29Ext.util.TaskRunner = function(interval){
30    interval = interval || 10;
31    var tasks = [], removeQueue = [];
32    var id = 0;
33    var running = false;
34
35    // private
36    var stopThread = function(){
37        running = false;
38        clearInterval(id);
39        id = 0;
40    };
41
42    // private
43    var startThread = function(){
44        if(!running){
45            running = true;
46            id = setInterval(runTasks, interval);
47        }
48    };
49
50    // private
51    var removeTask = function(t){
52        removeQueue.push(t);
53        if(t.onStop){
54            t.onStop.apply(t.scope || t);
55        }
56    };
57
58    // private
59    var runTasks = function(){
60        if(removeQueue.length > 0){
61            for(var i = 0, len = removeQueue.length; i < len; i++){
62                tasks.remove(removeQueue[i]);
63            }
64            removeQueue = [];
65            if(tasks.length < 1){
66                stopThread();
67                return;
68            }
69        }
70        var now = new Date().getTime();
71        for(var i = 0, len = tasks.length; i < len; ++i){
72            var t = tasks[i];
73            var itime = now - t.taskRunTime;
74            if(t.interval <= itime){
75                var rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
76                t.taskRunTime = now;
77                if(rt === false || t.taskRunCount === t.repeat){
78                    removeTask(t);
79                    return;
80                }
81            }
82            if(t.duration && t.duration <= (now - t.taskStartTime)){
83                removeTask(t);
84            }
85        }
86    };
87
88    /**
89     * @member Ext.util.TaskRunner
90     * @method start
91     * Starts a new task.
92     * @param {Object} task A config object that supports the following properties:<ul>
93     * <li><code>run</code> : Function<div class="sub-desc">The function to execute each time the task is run. The
94     * function will be called at each interval and passed the <code>args</code> argument if specified.  If a
95     * particular scope is required, be sure to specify it using the <code>scope</scope> argument.</div></li>
96     * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
97     * should be executed.</div></li>
98     * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
99     * specified by <code>run</code>.</div></li>
100     * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope in which to execute the
101     * <code>run</code> function.</div></li>
102     * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to execute
103     * the task before stopping automatically (defaults to indefinite).</div></li>
104     * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to execute the task before
105     * stopping automatically (defaults to indefinite).</div></li>
106     * </ul>
107     * @return {Object} The task
108     */
109    this.start = function(task){
110        tasks.push(task);
111        task.taskStartTime = new Date().getTime();
112        task.taskRunTime = 0;
113        task.taskRunCount = 0;
114        startThread();
115        return task;
116    };
117
118    /**
119     * @member Ext.util.TaskRunner
120     * @method stop
121     * Stops an existing running task.
122     * @param {Object} task The task to stop
123     * @return {Object} The task
124     */
125    this.stop = function(task){
126        removeTask(task);
127        return task;
128    };
129
130    /**
131     * @member Ext.util.TaskRunner
132     * @method stopAll
133     * Stops all tasks that are currently running.
134     */
135    this.stopAll = function(){
136        stopThread();
137        for(var i = 0, len = tasks.length; i < len; i++){
138            if(tasks[i].onStop){
139                tasks[i].onStop();
140            }
141        }
142        tasks = [];
143        removeQueue = [];
144    };
145};
146
147/**
148 * @class Ext.TaskMgr
149 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks.  See
150 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
151 * <pre><code>
152// Start a simple clock task that updates a div once per second
153var task = {
154    run: function(){
155        Ext.fly('clock').update(new Date().format('g:i:s A'));
156    },
157    interval: 1000 //1 second
158}
159Ext.TaskMgr.start(task);
160</code></pre>
161 * @singleton
162 */
163Ext.TaskMgr = new Ext.util.TaskRunner();
Note: See TracBrowser for help on using the repository browser.