updater.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var states = {};
  20. function update(obj, name, isWorking, interval){
  21. if(typeof isWorking == 'string' && !obj.get(isWorking)){
  22. return false;
  23. }
  24. var state = states[name];
  25. if(!state){
  26. var callback = function(){
  27. update(obj, name, isWorking, interval);
  28. };
  29. states[name] = state = {
  30. timeout: null,
  31. func: function(){
  32. if(typeof isWorking == 'string' && !obj.get(isWorking)){
  33. return false;
  34. }
  35. obj[name](callback);
  36. return true;
  37. },
  38. interval: interval,
  39. callback: callback
  40. };
  41. }
  42. clearTimeout(state.timeout);
  43. state.timeout = setTimeout(state.func, state.interval);
  44. return true;
  45. }
  46. function rerun(name){
  47. var state = states[name];
  48. if(state){
  49. clearTimeout(state.timeout);
  50. state.func();
  51. }
  52. }
  53. App.updater = {
  54. /**
  55. * Run function periodically with <code>App.contentUpdateInterval</code> delay.
  56. * Example 1(wrong way, will not be working):
  57. * var obj = {
  58. * method: function(callback){
  59. * //do something
  60. * }
  61. * };
  62. * App.updater.run(obj, 'method');
  63. *
  64. * Will be called only once, because <code>callback</code> will never execute. Below is right way:
  65. *
  66. * Example 2:
  67. * var obj = {
  68. * method: function(callback){
  69. * //do something
  70. * callback();
  71. * }
  72. * };
  73. * App.updater.run(obj, 'method');
  74. *
  75. * Method will always be called.
  76. *
  77. * Example 3:
  78. * var obj = {
  79. * method: function(callback){
  80. * //do something
  81. * callback();
  82. * },
  83. * isWorking: true
  84. * };
  85. * App.updater.run(obj, 'method', 'isWorking');
  86. *
  87. * <code>obj.method</code> will be called automatically.
  88. * Warning: You should call <code>callback</code> parameter when function finished iteration.
  89. * Otherwise nothing happened next time.
  90. * If <code>isWorking</code> provided, library will check <code>obj.isWorking</code> before iteration and
  91. * stop working when it equals to false. Otherwise method will always be called.
  92. *
  93. *
  94. *
  95. * @param obj Object
  96. * @param name Method name
  97. * @param isWorking Property, which will be checked as a rule for working
  98. * @param interval Interval between calls
  99. * @return {*}
  100. */
  101. run: function(obj, name, isWorking, interval){
  102. interval = interval || App.contentUpdateInterval;
  103. return update(obj, name, isWorking, interval);
  104. },
  105. /**
  106. * Immediate run function, which is periodically running using <code>run</code> method
  107. * Example:
  108. * App.updater.run(obj, 'clickIt');
  109. * App.updater.immediateRun('clickIt');
  110. *
  111. * <code>clickIt</code> will be executed immediately and proceed executing periodically
  112. *
  113. * @param name Method name, which was used as a parameter in <code>run</code> method
  114. * @return {*}
  115. */
  116. immediateRun: function(name){
  117. return rerun(name);
  118. },
  119. /**
  120. * Update interval for state by its name.
  121. *
  122. * @param name - state name
  123. * @param value - new interval value
  124. **/
  125. updateInterval: function(name, value) {
  126. if (!states[name]) return;
  127. states[name].interval = value;
  128. }
  129. };