updater.js 3.4 KB

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