Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / TaskManager.html
diff --git a/docs/source/TaskManager.html b/docs/source/TaskManager.html
new file mode 100644 (file)
index 0000000..602f9ed
--- /dev/null
@@ -0,0 +1,182 @@
+<!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.TaskRunner-method-constructor'><span id='Ext-util.TaskRunner'>/**
+</span></span> * @class Ext.util.TaskRunner
+ * Provides the ability to execute one or more arbitrary tasks in a multithreaded
+ * manner.  Generally, you can use the singleton {@link Ext.TaskManager} instead, but
+ * if needed, you can create separate instances of TaskRunner.  Any number of
+ * separate tasks can be started at any time and will run independently of each
+ * other. Example usage:
+ * &lt;pre&gt;&lt;code&gt;
+// Start a simple clock task that updates a div once per second
+var updateClock = function(){
+    Ext.fly('clock').update(new Date().format('g:i:s A'));
+} 
+var task = {
+    run: updateClock,
+    interval: 1000 //1 second
+}
+var runner = new Ext.util.TaskRunner();
+runner.start(task);
+
+// equivalent using TaskManager
+Ext.TaskManager.start({
+    run: updateClock,
+    interval: 1000
+});
+
+ * &lt;/code&gt;&lt;/pre&gt;
+ * &lt;p&gt;See the {@link #start} method for details about how to configure a task object.&lt;/p&gt;
+ * Also see {@link Ext.util.DelayedTask}. 
+ * 
+ * @constructor
+ * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
+ * (defaults to 10)
+ */
+Ext.ns('Ext.util');
+
+Ext.util.TaskRunner = function(interval) {
+    interval = interval || 10;
+    var tasks = [],
+    removeQueue = [],
+    id = 0,
+    running = false,
+
+    // private
+    stopThread = function() {
+        running = false;
+        clearInterval(id);
+        id = 0;
+    },
+
+    // private
+    startThread = function() {
+        if (!running) {
+            running = true;
+            id = setInterval(runTasks, interval);
+        }
+    },
+
+    // private
+    removeTask = function(t) {
+        removeQueue.push(t);
+        if (t.onStop) {
+            t.onStop.apply(t.scope || t);
+        }
+    },
+
+    // private
+    runTasks = function() {
+        var rqLen = removeQueue.length,
+            now = new Date().getTime(),
+            i;
+
+        if (rqLen &gt; 0) {
+            for (i = 0; i &lt; rqLen; i++) {
+                Ext.Array.remove(tasks, removeQueue[i]);
+            }
+            removeQueue = [];
+            if (tasks.length &lt; 1) {
+                stopThread();
+                return;
+            }
+        }
+        i = 0;
+        var t,
+            itime,
+            rt,
+            len = tasks.length;
+        for (; i &lt; len; ++i) {
+            t = tasks[i];
+            itime = now - t.taskRunTime;
+            if (t.interval &lt;= itime) {
+                rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
+                t.taskRunTime = now;
+                if (rt === false || t.taskRunCount === t.repeat) {
+                    removeTask(t);
+                    return;
+                }
+            }
+            if (t.duration &amp;&amp; t.duration &lt;= (now - t.taskStartTime)) {
+                removeTask(t);
+            }
+        }
+    };
+
+<span id='Ext-util.TaskRunner-method-start'>    /**
+</span>     * Starts a new task.
+     * @method start
+     * @param {Object} task &lt;p&gt;A config object that supports the following properties:&lt;ul&gt;
+     * &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
+     * function will be called at each interval and passed the &lt;code&gt;args&lt;/code&gt; argument if specified, and the
+     * current invocation count if not.&lt;/p&gt;
+     * &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;
+     * &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;
+     * &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
+     * should be invoked.&lt;/div&gt;&lt;/li&gt;
+     * &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
+     * specified by &lt;code&gt;run&lt;/code&gt;. If not specified, the current invocation count is passed.&lt;/div&gt;&lt;/li&gt;
+     * &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
+     * &lt;code&gt;run&lt;/code&gt; function. Defaults to the task config object.&lt;/div&gt;&lt;/li&gt;
+     * &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
+     * the task before stopping automatically (defaults to indefinite).&lt;/div&gt;&lt;/li&gt;
+     * &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
+     * stopping automatically (defaults to indefinite).&lt;/div&gt;&lt;/li&gt;
+     * &lt;/ul&gt;&lt;/p&gt;
+     * &lt;p&gt;Before each invocation, Ext injects the property &lt;code&gt;taskRunCount&lt;/code&gt; into the task object so
+     * that calculations based on the repeat count can be performed.&lt;/p&gt;
+     * @return {Object} The task
+     */
+    this.start = function(task) {
+        tasks.push(task);
+        task.taskStartTime = new Date().getTime();
+        task.taskRunTime = 0;
+        task.taskRunCount = 0;
+        startThread();
+        return task;
+    };
+
+<span id='Ext-util.TaskRunner-method-stop'>    /**
+</span>     * Stops an existing running task.
+     * @method stop
+     * @param {Object} task The task to stop
+     * @return {Object} The task
+     */
+    this.stop = function(task) {
+        removeTask(task);
+        return task;
+    };
+
+<span id='Ext-util.TaskRunner-method-stopAll'>    /**
+</span>     * Stops all tasks that are currently running.
+     * @method stopAll
+     */
+    this.stopAll = function() {
+        stopThread();
+        for (var i = 0, len = tasks.length; i &lt; len; i++) {
+            if (tasks[i].onStop) {
+                tasks[i].onStop();
+            }
+        }
+        tasks = [];
+        removeQueue = [];
+    };
+};
+
+<span id='Ext-TaskManager'>/**
+</span> * @class Ext.TaskManager
+ * @extends Ext.util.TaskRunner
+ * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks.  See
+ * {@link Ext.util.TaskRunner} for supported methods and task config properties.
+ * &lt;pre&gt;&lt;code&gt;
+// Start a simple clock task that updates a div once per second
+var task = {
+    run: function(){
+        Ext.fly('clock').update(new Date().format('g:i:s A'));
+    },
+    interval: 1000 //1 second
+}
+Ext.TaskManager.start(task);
+&lt;/code&gt;&lt;/pre&gt;
+ * &lt;p&gt;See the {@link #start} method for details about how to configure a task object.&lt;/p&gt;
+ * @singleton
+ */
+Ext.TaskManager = Ext.create('Ext.util.TaskRunner');</pre></pre></body></html>
\ No newline at end of file