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=10] The minimum precision in milliseconds supported by this TaskRunner instance
49 Ext.util.TaskRunner = function(interval) {
50 interval = interval || 10;
57 stopThread = function() {
64 startThread = function() {
67 id = setInterval(runTasks, interval);
72 removeTask = function(t) {
75 t.onStop.apply(t.scope || t);
80 runTasks = function() {
81 var rqLen = removeQueue.length,
82 now = new Date().getTime(),
86 for (i = 0; i < rqLen; i++) {
87 Ext.Array.remove(tasks, removeQueue[i]);
90 if (tasks.length < 1) {
100 for (; i < len; ++i) {
102 itime = now - t.taskRunTime;
103 if (t.interval <= itime) {
104 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
106 if (rt === false || t.taskRunCount === t.repeat) {
111 if (t.duration && t.duration <= (now - t.taskStartTime)) {
120 * @param {Object} task <p>A config object that supports the following properties:<ul>
121 * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
122 * function will be called at each interval and passed the <code>args</code> argument if specified, and the
123 * current invocation count if not.</p>
124 * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
125 * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
126 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
127 * should be invoked.</div></li>
128 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
129 * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
130 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
131 * <code>run</code> function. Defaults to the task config object.</div></li>
132 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
133 * the task before stopping automatically (defaults to indefinite).</div></li>
134 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
135 * stopping automatically (defaults to indefinite).</div></li>
137 * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
138 * that calculations based on the repeat count can be performed.</p>
139 * @return {Object} The task
141 this.start = function(task) {
143 task.taskStartTime = new Date().getTime();
144 task.taskRunTime = 0;
145 task.taskRunCount = 0;
151 * Stops an existing running task.
153 * @param {Object} task The task to stop
154 * @return {Object} The task
156 this.stop = function(task) {
162 * Stops all tasks that are currently running.
165 this.stopAll = function() {
167 for (var i = 0, len = tasks.length; i < len; i++) {
168 if (tasks[i].onStop) {
178 * @class Ext.TaskManager
179 * @extends Ext.util.TaskRunner
180 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
181 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
183 // Start a simple clock task that updates a div once per second
186 Ext.fly('clock').update(new Date().format('g:i:s A'));
188 interval: 1000 //1 second
190 Ext.TaskManager.start(task);
192 * <p>See the {@link #start} method for details about how to configure a task object.</p>
195 Ext.TaskManager = Ext.create('Ext.util.TaskRunner');