You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.4 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/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.TaskMgr} 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. // equivalent using TaskMgr
  37. Ext.TaskMgr.start({
  38. run: updateClock,
  39. interval: 1000
  40. });
  41. * &lt;/code&gt;&lt;/pre&gt;
  42. * &lt;p&gt;See the {@link #start} method for details about how to configure a task object.&lt;/p&gt;
  43. * Also see {@link Ext.util.DelayedTask}.
  44. *
  45. * @constructor
  46. * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
  47. * (defaults to 10)
  48. */
  49. Ext.util.TaskRunner = function(interval){
  50. interval = interval || 10;
  51. var tasks = [],
  52. removeQueue = [],
  53. id = 0,
  54. running = false,
  55. // private
  56. stopThread = function(){
  57. running = false;
  58. clearInterval(id);
  59. id = 0;
  60. },
  61. // private
  62. startThread = function(){
  63. if(!running){
  64. running = true;
  65. id = setInterval(runTasks, interval);
  66. }
  67. },
  68. // private
  69. removeTask = function(t){
  70. removeQueue.push(t);
  71. if(t.onStop){
  72. t.onStop.apply(t.scope || t);
  73. }
  74. },
  75. // private
  76. runTasks = function(){
  77. var rqLen = removeQueue.length,
  78. now = new Date().getTime();
  79. if(rqLen &gt; 0){
  80. for(var i = 0; i &lt; rqLen; i++){
  81. tasks.remove(removeQueue[i]);
  82. }
  83. removeQueue = [];
  84. if(tasks.length &lt; 1){
  85. stopThread();
  86. return;
  87. }
  88. }
  89. for(var i = 0, t, itime, rt, len = tasks.length; i &lt; len; ++i){
  90. t = tasks[i];
  91. itime = now - t.taskRunTime;
  92. if(t.interval &lt;= itime){
  93. rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
  94. t.taskRunTime = now;
  95. if(rt === false || t.taskRunCount === t.repeat){
  96. removeTask(t);
  97. return;
  98. }
  99. }
  100. if(t.duration &amp;&amp; t.duration &lt;= (now - t.taskStartTime)){
  101. removeTask(t);
  102. }
  103. }
  104. };
  105. <span id='Ext-util-TaskRunner-method-start'> /**
  106. </span> * Starts a new task.
  107. * @method start
  108. * @param {Object} task &lt;p&gt;A config object that supports the following properties:&lt;ul&gt;
  109. * &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
  110. * function will be called at each interval and passed the &lt;code&gt;args&lt;/code&gt; argument if specified, and the
  111. * current invocation count if not.&lt;/p&gt;
  112. * &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;
  113. * &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;
  114. * &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
  115. * should be invoked.&lt;/div&gt;&lt;/li&gt;
  116. * &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
  117. * specified by &lt;code&gt;run&lt;/code&gt;. If not specified, the current invocation count is passed.&lt;/div&gt;&lt;/li&gt;
  118. * &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
  119. * &lt;code&gt;run&lt;/code&gt; function. Defaults to the task config object.&lt;/div&gt;&lt;/li&gt;
  120. * &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
  121. * the task before stopping automatically (defaults to indefinite).&lt;/div&gt;&lt;/li&gt;
  122. * &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
  123. * stopping automatically (defaults to indefinite).&lt;/div&gt;&lt;/li&gt;
  124. * &lt;/ul&gt;&lt;/p&gt;
  125. * &lt;p&gt;Before each invocation, Ext injects the property &lt;code&gt;taskRunCount&lt;/code&gt; into the task object so
  126. * that calculations based on the repeat count can be performed.&lt;/p&gt;
  127. * @return {Object} The task
  128. */
  129. this.start = function(task){
  130. tasks.push(task);
  131. task.taskStartTime = new Date().getTime();
  132. task.taskRunTime = 0;
  133. task.taskRunCount = 0;
  134. startThread();
  135. return task;
  136. };
  137. <span id='Ext-util-TaskRunner-method-stop'> /**
  138. </span> * Stops an existing running task.
  139. * @method stop
  140. * @param {Object} task The task to stop
  141. * @return {Object} The task
  142. */
  143. this.stop = function(task){
  144. removeTask(task);
  145. return task;
  146. };
  147. <span id='Ext-util-TaskRunner-method-stopAll'> /**
  148. </span> * Stops all tasks that are currently running.
  149. * @method stopAll
  150. */
  151. this.stopAll = function(){
  152. stopThread();
  153. for(var i = 0, len = tasks.length; i &lt; len; i++){
  154. if(tasks[i].onStop){
  155. tasks[i].onStop();
  156. }
  157. }
  158. tasks = [];
  159. removeQueue = [];
  160. };
  161. };
  162. <span id='Ext-TaskMgr'>/**
  163. </span> * @class Ext.TaskMgr
  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.
  167. * &lt;pre&gt;&lt;code&gt;
  168. // Start a simple clock task that updates a div once per second
  169. var task = {
  170. run: function(){
  171. Ext.fly('clock').update(new Date().format('g:i:s A'));
  172. },
  173. interval: 1000 //1 second
  174. }
  175. Ext.TaskMgr.start(task);
  176. &lt;/code&gt;&lt;/pre&gt;
  177. * &lt;p&gt;See the {@link #start} method for details about how to configure a task object.&lt;/p&gt;
  178. * @singleton
  179. */
  180. Ext.TaskMgr = new Ext.util.TaskRunner();</pre>
  181. </body>
  182. </html>