Web UI for ARGUS server
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.

189 lines
6.6 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>The source code</title>
  5. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  6. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  7. </head>
  8. <body onload="prettyPrint();">
  9. <pre class="prettyprint lang-js">/*!
  10. * Ext JS Library 3.3.1
  11. * Copyright(c) 2006-2010 Sencha Inc.
  12. * licensing@sencha.com
  13. * http://www.sencha.com/license
  14. */
  15. <div id="cls-Ext.util.TaskRunner"></div>/**
  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.TaskMgr} 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:
  22. * <pre><code>
  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'));
  26. }
  27. var task = {
  28. run: updateClock,
  29. interval: 1000 //1 second
  30. }
  31. var runner = new Ext.util.TaskRunner();
  32. runner.start(task);
  33. // equivalent using TaskMgr
  34. Ext.TaskMgr.start({
  35. run: updateClock,
  36. interval: 1000
  37. });
  38. * </code></pre>
  39. * <p>See the {@link #start} method for details about how to configure a task object.</p>
  40. * Also see {@link Ext.util.DelayedTask}.
  41. *
  42. * @constructor
  43. * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
  44. * (defaults to 10)
  45. */
  46. Ext.util.TaskRunner = function(interval){
  47. interval = interval || 10;
  48. var tasks = [],
  49. removeQueue = [],
  50. id = 0,
  51. running = false,
  52. // private
  53. stopThread = function(){
  54. running = false;
  55. clearInterval(id);
  56. id = 0;
  57. },
  58. // private
  59. startThread = function(){
  60. if(!running){
  61. running = true;
  62. id = setInterval(runTasks, interval);
  63. }
  64. },
  65. // private
  66. removeTask = function(t){
  67. removeQueue.push(t);
  68. if(t.onStop){
  69. t.onStop.apply(t.scope || t);
  70. }
  71. },
  72. // private
  73. runTasks = function(){
  74. var rqLen = removeQueue.length,
  75. now = new Date().getTime();
  76. if(rqLen > 0){
  77. for(var i = 0; i < rqLen; i++){
  78. tasks.remove(removeQueue[i]);
  79. }
  80. removeQueue = [];
  81. if(tasks.length < 1){
  82. stopThread();
  83. return;
  84. }
  85. }
  86. for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
  87. t = tasks[i];
  88. itime = now - t.taskRunTime;
  89. if(t.interval <= itime){
  90. rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
  91. t.taskRunTime = now;
  92. if(rt === false || t.taskRunCount === t.repeat){
  93. removeTask(t);
  94. return;
  95. }
  96. }
  97. if(t.duration && t.duration <= (now - t.taskStartTime)){
  98. removeTask(t);
  99. }
  100. }
  101. };
  102. <div id="method-Ext.util.TaskRunner-start"></div>/**
  103. * Starts a new task.
  104. * @method start
  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>
  121. * </ul></p>
  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
  125. */
  126. this.start = function(task){
  127. tasks.push(task);
  128. task.taskStartTime = new Date().getTime();
  129. task.taskRunTime = 0;
  130. task.taskRunCount = 0;
  131. startThread();
  132. return task;
  133. };
  134. <div id="method-Ext.util.TaskRunner-stop"></div>/**
  135. * Stops an existing running task.
  136. * @method stop
  137. * @param {Object} task The task to stop
  138. * @return {Object} The task
  139. */
  140. this.stop = function(task){
  141. removeTask(task);
  142. return task;
  143. };
  144. <div id="method-Ext.util.TaskRunner-stopAll"></div>/**
  145. * Stops all tasks that are currently running.
  146. * @method stopAll
  147. */
  148. this.stopAll = function(){
  149. stopThread();
  150. for(var i = 0, len = tasks.length; i < len; i++){
  151. if(tasks[i].onStop){
  152. tasks[i].onStop();
  153. }
  154. }
  155. tasks = [];
  156. removeQueue = [];
  157. };
  158. };
  159. <div id="cls-Ext.TaskMgr"></div>/**
  160. * @class Ext.TaskMgr
  161. * @extends Ext.util.TaskRunner
  162. * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
  163. * {@link Ext.util.TaskRunner} for supported methods and task config properties.
  164. * <pre><code>
  165. // Start a simple clock task that updates a div once per second
  166. var task = {
  167. run: function(){
  168. Ext.fly('clock').update(new Date().format('g:i:s A'));
  169. },
  170. interval: 1000 //1 second
  171. }
  172. Ext.TaskMgr.start(task);
  173. </code></pre>
  174. * <p>See the {@link #start} method for details about how to configure a task object.</p>
  175. * @singleton
  176. */
  177. Ext.TaskMgr = new Ext.util.TaskRunner();</pre>
  178. </body>
  179. </html>