updater.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, urlPattern){
  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, urlPattern);
  28. };
  29. states[name] = state = {
  30. timeout: null,
  31. func: function(){
  32. var urlRegExp = new RegExp(urlPattern);
  33. if(typeof isWorking == 'string' && !obj.get(isWorking)){
  34. return false;
  35. }
  36. if (urlRegExp.test(App.router.get('location.lastSetURL'))) {
  37. obj[name](callback);
  38. } else {
  39. callback();
  40. }
  41. return true;
  42. },
  43. interval: interval,
  44. callback: callback
  45. };
  46. }
  47. clearTimeout(state.timeout);
  48. state.timeout = setTimeout(state.func, state.interval);
  49. return true;
  50. }
  51. function rerun(name){
  52. var state = states[name];
  53. if(state){
  54. clearTimeout(state.timeout);
  55. state.func();
  56. }
  57. }
  58. App.updater = {
  59. /**
  60. * Run function periodically with <code>App.contentUpdateInterval</code> delay.
  61. * Example 1(wrong way, will not be working):
  62. * var obj = {
  63. * method: function(callback){
  64. * //do something
  65. * }
  66. * };
  67. * App.updater.run(obj, 'method');
  68. *
  69. * Will be called only once, because <code>callback</code> will never execute. Below is right way:
  70. *
  71. * Example 2:
  72. * var obj = {
  73. * method: function(callback){
  74. * //do something
  75. * callback();
  76. * }
  77. * };
  78. * App.updater.run(obj, 'method');
  79. *
  80. * Method will always be called.
  81. *
  82. * Example 3:
  83. * var obj = {
  84. * method: function(callback){
  85. * //do something
  86. * callback();
  87. * },
  88. * isWorking: true
  89. * };
  90. * App.updater.run(obj, 'method', 'isWorking');
  91. *
  92. * <code>obj.method</code> will be called automatically.
  93. * Warning: You should call <code>callback</code> parameter when function finished iteration.
  94. * Otherwise nothing happened next time.
  95. * If <code>isWorking</code> provided, library will check <code>obj.isWorking</code> before iteration and
  96. * stop working when it equals to false. Otherwise method will always be called.
  97. *
  98. *
  99. *
  100. * @param obj Object
  101. * @param name Method name
  102. * @param isWorking Property, which will be checked as a rule for working
  103. * @param interval Interval between calls
  104. * @param urlPattern Pattern to match allowed urls
  105. * @param [interval] Interval between calls
  106. * @return {*}
  107. */
  108. run: function(obj, name, isWorking, interval, urlPattern){
  109. interval = interval || App.contentUpdateInterval;
  110. return update(obj, name, isWorking, interval, urlPattern);
  111. },
  112. /**
  113. * Immediate run function, which is periodically running using <code>run</code> method
  114. * Example:
  115. * App.updater.run(obj, 'clickIt');
  116. * App.updater.immediateRun('clickIt');
  117. *
  118. * <code>clickIt</code> will be executed immediately and proceed executing periodically
  119. *
  120. * @param name Method name, which was used as a parameter in <code>run</code> method
  121. * @return {*}
  122. */
  123. immediateRun: function(name){
  124. return rerun(name);
  125. },
  126. /**
  127. * Update interval for state by its name.
  128. *
  129. * @param name - state name
  130. * @param value - new interval value
  131. **/
  132. updateInterval: function(name, value) {
  133. if (!states[name]) return;
  134. states[name].interval = value;
  135. }
  136. };