lazy_loading.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. */
  32. run: function (options) {
  33. var initSize = options.initSize || 25,
  34. chunkSize = options.chunkSize || 50,
  35. delay = options.delay || 300,
  36. destination = options.destination,
  37. source = options.source,
  38. context = options.context,
  39. chunks;
  40. if (Array.isArray(destination) && Array.isArray(source)) {
  41. destination.pushObjects(source.slice(0, initSize));
  42. if(source.length > initSize) {
  43. chunks = this.divideIntoChunks(source.slice(initSize, source.length), chunkSize);
  44. this.pushChunk(chunks, 0, delay, destination, context);
  45. } else {
  46. context.set('isLoaded', true);
  47. }
  48. } else {
  49. console.error('Lazy loading: source or destination has incorrect value');
  50. }
  51. },
  52. /**
  53. * push chunks into destination array in delay time
  54. * @param chunks
  55. * @param index
  56. * @param delay
  57. * @param destination
  58. * @param context
  59. */
  60. pushChunk: function (chunks, index, delay, destination, context) {
  61. var self = this;
  62. setTimeout(function () {
  63. destination.pushObjects(chunks[index]);
  64. if (chunks.length === (index + 1)) {
  65. context.set('isLoaded', true);
  66. }
  67. index++;
  68. self.pushChunk(chunks, index, delay, destination, context);
  69. }, delay);
  70. },
  71. /**
  72. * divide source array into chunks
  73. * @param source
  74. * @param chunkSize
  75. * @return {Array}
  76. */
  77. divideIntoChunks: function (source, chunkSize) {
  78. var chunk = [];
  79. var chunks = [];
  80. var counter = 0;
  81. source.forEach(function (item) {
  82. counter++;
  83. chunk.push(item);
  84. if (counter === chunkSize) {
  85. chunks.push(chunk);
  86. chunk = [];
  87. counter = 0;
  88. }
  89. });
  90. if (chunk.length > 0) {
  91. chunks.push(chunk);
  92. }
  93. return chunks;
  94. }
  95. };