3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.util.TaskRunner
9 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
10 * manner. Generally, you can use the singleton {@link Ext.TaskMgr} instead, but
11 * if needed, you can create separate instances of TaskRunner. Any number of
12 * separate tasks can be started at any time and will run independently of each
13 * other. Example usage:
15 // Start a simple clock task that updates a div once per second
16 var updateClock = function(){
17 Ext.fly('clock').update(new Date().format('g:i:s A'));
21 interval: 1000 //1 second
23 var runner = new Ext.util.TaskRunner();
26 // equivalent using TaskMgr
33 * <p>See the {@link #start} method for details about how to configure a task object.</p>
34 * Also see {@link Ext.util.DelayedTask}.
37 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
40 Ext.util.TaskRunner = function(interval){
41 interval = interval || 10;
48 stopThread = function(){
55 startThread = function(){
58 id = setInterval(runTasks, interval);
63 removeTask = function(t){
66 t.onStop.apply(t.scope || t);
71 runTasks = function(){
72 var rqLen = removeQueue.length,
73 now = new Date().getTime();
76 for(var i = 0; i < rqLen; i++){
77 tasks.remove(removeQueue[i]);
85 for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
87 itime = now - t.taskRunTime;
88 if(t.interval <= itime){
89 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
91 if(rt === false || t.taskRunCount === t.repeat){
96 if(t.duration && t.duration <= (now - t.taskStartTime)){
105 * @param {Object} task <p>A config object that supports the following properties:<ul>
106 * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
107 * function will be called at each interval and passed the <code>args</code> argument if specified, and the
108 * current invocation count if not.</p>
109 * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
110 * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
111 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
112 * should be invoked.</div></li>
113 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
114 * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
115 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
116 * <code>run</code> function. Defaults to the task config object.</div></li>
117 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
118 * the task before stopping automatically (defaults to indefinite).</div></li>
119 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
120 * stopping automatically (defaults to indefinite).</div></li>
122 * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
123 * that calculations based on the repeat count can be performed.</p>
124 * @return {Object} The task
126 this.start = function(task){
128 task.taskStartTime = new Date().getTime();
129 task.taskRunTime = 0;
130 task.taskRunCount = 0;
136 * Stops an existing running task.
138 * @param {Object} task The task to stop
139 * @return {Object} The task
141 this.stop = function(task){
147 * Stops all tasks that are currently running.
150 this.stopAll = function(){
152 for(var i = 0, len = tasks.length; i < len; i++){
164 * @extends Ext.util.TaskRunner
165 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
166 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
168 // Start a simple clock task that updates a div once per second
171 Ext.fly('clock').update(new Date().format('g:i:s A'));
173 interval: 1000 //1 second
175 Ext.TaskMgr.start(task);
177 * <p>See the {@link #start} method for details about how to configure a task object.</p>
180 Ext.TaskMgr = new Ext.util.TaskRunner();