updater.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. callback: callback
  39. };
  40. }
  41. clearTimeout(state.timeout);
  42. state.timeout = setTimeout(state.func, interval);
  43. return true;
  44. }
  45. function rerun(name){
  46. var state = states[name];
  47. if(state){
  48. clearTimeout(state.timeout);
  49. state.func();
  50. }
  51. }
  52. App.updater = {
  53. /**
  54. * Run function periodically with <code>App.contentUpdateInterval</code> delay.
  55. * Example 1(wrong way, will not be working):
  56. * var obj = {
  57. * method: function(callback){
  58. * //do something
  59. * }
  60. * };
  61. * App.updater.run(obj, 'method');
  62. *
  63. * Will be called only once, because <code>callback</code> will never execute. Below is right way:
  64. *
  65. * Example 2:
  66. * var obj = {
  67. * method: function(callback){
  68. * //do something
  69. * callback();
  70. * }
  71. * };
  72. * App.updater.run(obj, 'method');
  73. *
  74. * Method will always be called.
  75. *
  76. * Example 3:
  77. * var obj = {
  78. * method: function(callback){
  79. * //do something
  80. * callback();
  81. * },
  82. * isWorking: true
  83. * };
  84. * App.updater.run(obj, 'method', 'isWorking');
  85. *
  86. * <code>obj.method</code> will be called automatically.
  87. * Warning: You should call <code>callback</code> parameter when function finished iteration.
  88. * Otherwise nothing happened next time.
  89. * If <code>isWorking</code> provided, library will check <code>obj.isWorking</code> before iteration and
  90. * stop working when it equals to false. Otherwise method will always be called.
  91. *
  92. *
  93. *
  94. * @param obj Object
  95. * @param name Method name
  96. * @param isWorking Property, which will be checked as a rule for working
  97. * @param interval Interval between calls
  98. * @return {*}
  99. */
  100. run: function(obj, name, isWorking, interval){
  101. interval = interval || App.contentUpdateInterval;
  102. return update(obj, name, isWorking, interval);
  103. },
  104. /**
  105. * Immediate run function, which is periodically running using <code>run</code> method
  106. * Example:
  107. * App.updater.run(obj, 'clickIt');
  108. * App.updater.immediateRun('clickIt');
  109. *
  110. * <code>clickIt</code> will be executed immediately and proceed executing periodically
  111. *
  112. * @param name Method name, which was used as a parameter in <code>run</code> method
  113. * @return {*}
  114. */
  115. immediateRun: function(name){
  116. return rerun(name);
  117. }
  118. };