Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / docs / source / TaskMgr.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.0
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.util.TaskRunner"></div>/**
16  * @class Ext.util.TaskRunner
17  * Provides the ability to execute one or more arbitrary tasks in a multithreaded
18  * manner.  Generally, you can use the singleton {@link Ext.TaskMgr} instead, but
19  * if needed, you can create separate instances of TaskRunner.  Any number of
20  * separate tasks can be started at any time and will run independently of each
21  * other. Example usage:
22  * <pre><code>
23 // Start a simple clock task that updates a div once per second
24 var updateClock = function(){
25     Ext.fly('clock').update(new Date().format('g:i:s A'));
26
27 var task = {
28     run: updateClock,
29     interval: 1000 //1 second
30 }
31 var runner = new Ext.util.TaskRunner();
32 runner.start(task);
33
34 // equivalent using TaskMgr
35 Ext.TaskMgr.start({
36     run: updateClock,
37     interval: 1000
38 });
39
40  * </code></pre>
41  * <p>See the {@link #start} method for details about how to configure a task object.</p>
42  * Also see {@link Ext.util.DelayedTask}. 
43  * 
44  * @constructor
45  * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
46  * (defaults to 10)
47  */
48 Ext.util.TaskRunner = function(interval){
49     interval = interval || 10;
50     var tasks = [], 
51         removeQueue = [],
52         id = 0,
53         running = false,
54
55         // private
56         stopThread = function(){
57                 running = false;
58                 clearInterval(id);
59                 id = 0;
60             },
61
62         // private
63         startThread = function(){
64                 if(!running){
65                     running = true;
66                     id = setInterval(runTasks, interval);
67                 }
68             },
69
70         // private
71         removeTask = function(t){
72                 removeQueue.push(t);
73                 if(t.onStop){
74                     t.onStop.apply(t.scope || t);
75                 }
76             },
77             
78         // private
79         runTasks = function(){
80                 var rqLen = removeQueue.length,
81                         now = new Date().getTime();                                             
82             
83                 if(rqLen > 0){
84                     for(var i = 0; i < rqLen; i++){
85                         tasks.remove(removeQueue[i]);
86                     }
87                     removeQueue = [];
88                     if(tasks.length < 1){
89                         stopThread();
90                         return;
91                     }
92                 }               
93                 for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
94                     t = tasks[i];
95                     itime = now - t.taskRunTime;
96                     if(t.interval <= itime){
97                         rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
98                         t.taskRunTime = now;
99                         if(rt === false || t.taskRunCount === t.repeat){
100                             removeTask(t);
101                             return;
102                         }
103                     }
104                     if(t.duration && t.duration <= (now - t.taskStartTime)){
105                         removeTask(t);
106                     }
107                 }
108             };
109
110     <div id="method-Ext.util.TaskRunner-start"></div>/**
111      * Starts a new task.
112      * @method start
113      * @param {Object} task <p>A config object that supports the following properties:<ul>
114      * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
115      * function will be called at each interval and passed the <code>args</code> argument if specified, and the
116      * current invocation count if not.</p>
117      * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
118      * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
119      * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
120      * should be invoked.</div></li>
121      * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
122      * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
123      * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
124      * <code>run</code> function. Defaults to the task config object.</div></li>
125      * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
126      * the task before stopping automatically (defaults to indefinite).</div></li>
127      * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
128      * stopping automatically (defaults to indefinite).</div></li>
129      * </ul></p>
130      * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
131      * that calculations based on the repeat count can be performed.</p>
132      * @return {Object} The task
133      */
134     this.start = function(task){
135         tasks.push(task);
136         task.taskStartTime = new Date().getTime();
137         task.taskRunTime = 0;
138         task.taskRunCount = 0;
139         startThread();
140         return task;
141     };
142
143     <div id="method-Ext.util.TaskRunner-stop"></div>/**
144      * Stops an existing running task.
145      * @method stop
146      * @param {Object} task The task to stop
147      * @return {Object} The task
148      */
149     this.stop = function(task){
150         removeTask(task);
151         return task;
152     };
153
154     <div id="method-Ext.util.TaskRunner-stopAll"></div>/**
155      * Stops all tasks that are currently running.
156      * @method stopAll
157      */
158     this.stopAll = function(){
159         stopThread();
160         for(var i = 0, len = tasks.length; i < len; i++){
161             if(tasks[i].onStop){
162                 tasks[i].onStop();
163             }
164         }
165         tasks = [];
166         removeQueue = [];
167     };
168 };
169
170 <div id="cls-Ext.TaskMgr"></div>/**
171  * @class Ext.TaskMgr
172  * @extends Ext.util.TaskRunner
173  * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks.  See
174  * {@link Ext.util.TaskRunner} for supported methods and task config properties.
175  * <pre><code>
176 // Start a simple clock task that updates a div once per second
177 var task = {
178     run: function(){
179         Ext.fly('clock').update(new Date().format('g:i:s A'));
180     },
181     interval: 1000 //1 second
182 }
183 Ext.TaskMgr.start(task);
184 </code></pre>
185  * <p>See the {@link #start} method for details about how to configure a task object.</p>
186  * @singleton
187  */
188 Ext.TaskMgr = new Ext.util.TaskRunner();</pre>    
189 </body>
190 </html>