updater.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. App.updater = {
  42. /**
  43. * Run function periodically with <code>App.contentUpdateInterval</code> delay.
  44. * Example 1(wrong way, will not be working):
  45. * var obj = {
  46. * method: function(callback){
  47. * //do something
  48. * }
  49. * };
  50. * App.updater.run(obj, 'method');
  51. *
  52. * Will be called only once, because <code>callback</code> will never execute. Below is right way:
  53. *
  54. * Example 2:
  55. * var obj = {
  56. * method: function(callback){
  57. * //do something
  58. * callback();
  59. * }
  60. * };
  61. * App.updater.run(obj, 'method');
  62. *
  63. * Method will always be called.
  64. *
  65. * Example 3:
  66. * var obj = {
  67. * method: function(callback){
  68. * //do something
  69. * callback();
  70. * },
  71. * isWorking: true
  72. * };
  73. * App.updater.run(obj, 'method', 'isWorking');
  74. *
  75. * <code>obj.method</code> will be called automatically.
  76. * Warning: You should call <code>callback</code> parameter when function finished iteration.
  77. * Otherwise nothing happened next time.
  78. * If <code>isWorking</code> provided, library will check <code>obj.isWorking</code> before iteration and
  79. * stop working when it equals to false. Otherwise method will always be called.
  80. *
  81. *
  82. *
  83. * @param obj Object
  84. * @param name Method name
  85. * @param isWorking Property, which will be checked as a rule for working
  86. * @return {*}
  87. */
  88. run: function(obj, name, isWorking){
  89. return update(obj, name, isWorking);
  90. }
  91. }