3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
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.TaskManager} 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:
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'));
29 interval: 1000 //1 second
31 var runner = new Ext.util.TaskRunner();
34 // equivalent using TaskManager
35 Ext.TaskManager.start({
41 * <p>See the {@link #start} method for details about how to configure a task object.</p>
42 * Also see {@link Ext.util.DelayedTask}.
45 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
50 Ext.util.TaskRunner = function(interval) {
51 interval = interval || 10;
58 stopThread = function() {
65 startThread = function() {
68 id = setInterval(runTasks, interval);
73 removeTask = function(t) {
76 t.onStop.apply(t.scope || t);
81 runTasks = function() {
82 var rqLen = removeQueue.length,
83 now = new Date().getTime(),
87 for (i = 0; i < rqLen; i++) {
88 Ext.Array.remove(tasks, removeQueue[i]);
91 if (tasks.length < 1) {
101 for (; i < len; ++i) {
103 itime = now - t.taskRunTime;
104 if (t.interval <= itime) {
105 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
107 if (rt === false || t.taskRunCount === t.repeat) {
112 if (t.duration && t.duration <= (now - t.taskStartTime)) {
121 * @param {Object} task <p>A config object that supports the following properties:<ul>
122 * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
123 * function will be called at each interval and passed the <code>args</code> argument if specified, and the
124 * current invocation count if not.</p>
125 * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
126 * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
127 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
128 * should be invoked.</div></li>
129 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
130 * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
131 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
132 * <code>run</code> function. Defaults to the task config object.</div></li>
133 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
134 * the task before stopping automatically (defaults to indefinite).</div></li>
135 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
136 * stopping automatically (defaults to indefinite).</div></li>
138 * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
139 * that calculations based on the repeat count can be performed.</p>
140 * @return {Object} The task
142 this.start = function(task) {
144 task.taskStartTime = new Date().getTime();
145 task.taskRunTime = 0;
146 task.taskRunCount = 0;
152 * Stops an existing running task.
154 * @param {Object} task The task to stop
155 * @return {Object} The task
157 this.stop = function(task) {
163 * Stops all tasks that are currently running.
166 this.stopAll = function() {
168 for (var i = 0, len = tasks.length; i < len; i++) {
169 if (tasks[i].onStop) {
179 * @class Ext.TaskManager
180 * @extends Ext.util.TaskRunner
181 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
182 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
184 // Start a simple clock task that updates a div once per second
187 Ext.fly('clock').update(new Date().format('g:i:s A'));
189 interval: 1000 //1 second
191 Ext.TaskManager.start(task);
193 * <p>See the {@link #start} method for details about how to configure a task object.</p>
196 Ext.TaskManager = Ext.create('Ext.util.TaskRunner');