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