lazy_loading.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. module.exports = {
  19. /**
  20. * Divide source array in chunks, then push each chunk into destination array
  21. * with delay time one by one. So then destination array gets more and more items
  22. * till all items are loaded.
  23. * @param options
  24. * options.initSize - number of items which will be pushed in array immediately
  25. * options.chunkSize - number of items which will be pushed after defined delay
  26. * options.delay - interval between each chunk push
  27. * options.destination - array where items will be pushed
  28. * options.source - source of items
  29. * options.context - the object that should know when data is completely loaded,
  30. * lazy loading will define "isLoaded" property for context object and update it
  31. * @return {Object} - instance of lazy loading run
  32. */
  33. run: function (options) {
  34. var initSize = options.initSize || 25,
  35. chunkSize = options.chunkSize || 50,
  36. delay = options.delay || 300,
  37. destination = options.destination,
  38. source = options.source,
  39. self = this,
  40. instance = {
  41. context: options.context,
  42. timeoutRef: null
  43. },
  44. chunks;
  45. if (Array.isArray(destination) && Array.isArray(source)) {
  46. destination.pushObjects(source.slice(0, initSize));
  47. if(source.length > initSize) {
  48. chunks = this.divideIntoChunks(source.slice(initSize, source.length), chunkSize);
  49. this.pushChunk(chunks, 0, delay, destination, instance);
  50. } else {
  51. instance.context.set('isLoaded', true);
  52. }
  53. } else {
  54. console.error('Lazy loading: source or destination has incorrect value');
  55. }
  56. return instance;
  57. },
  58. /**
  59. * push chunks into destination array in delay time
  60. * @param chunks
  61. * @param index
  62. * @param delay
  63. * @param destination
  64. * @param instance
  65. */
  66. pushChunk: function (chunks, index, delay, destination, instance) {
  67. var self = this;
  68. instance.timeoutRef = setTimeout(function () {
  69. destination.pushObjects(chunks[index]);
  70. if (chunks.length === (++index)) {
  71. instance.context.set('isLoaded', true);
  72. } else {
  73. self.pushChunk(chunks, index, delay, destination, instance);
  74. }
  75. }, delay);
  76. },
  77. /**
  78. * divide source array into chunks
  79. * @param source
  80. * @param chunkSize
  81. * @return {Array}
  82. */
  83. divideIntoChunks: function (source, chunkSize) {
  84. var chunk = [];
  85. var chunks = [];
  86. var counter = 0;
  87. source.forEach(function (item) {
  88. counter++;
  89. chunk.push(item);
  90. if (counter === chunkSize) {
  91. chunks.push(chunk);
  92. chunk = [];
  93. counter = 0;
  94. }
  95. });
  96. if (chunk.length > 0) {
  97. chunks.push(chunk);
  98. }
  99. return chunks;
  100. },
  101. /**
  102. * terminate lazy loading run
  103. * @param instance - lazy loading instance
  104. */
  105. terminate: function (instance) {
  106. clearTimeout(instance.timeoutRef);
  107. }
  108. };