pdf_rendering_queue.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFRenderingQueue = exports.RenderingStates = void 0;
  27. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  28. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  29. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  30. var CLEANUP_TIMEOUT = 30000;
  31. var RenderingStates = {
  32. INITIAL: 0,
  33. RUNNING: 1,
  34. PAUSED: 2,
  35. FINISHED: 3
  36. };
  37. exports.RenderingStates = RenderingStates;
  38. var PDFRenderingQueue =
  39. /*#__PURE__*/
  40. function () {
  41. function PDFRenderingQueue() {
  42. _classCallCheck(this, PDFRenderingQueue);
  43. this.pdfViewer = null;
  44. this.pdfThumbnailViewer = null;
  45. this.onIdle = null;
  46. this.highestPriorityPage = null;
  47. this.idleTimeout = null;
  48. this.printing = false;
  49. this.isThumbnailViewEnabled = false;
  50. }
  51. _createClass(PDFRenderingQueue, [{
  52. key: "setViewer",
  53. value: function setViewer(pdfViewer) {
  54. this.pdfViewer = pdfViewer;
  55. }
  56. }, {
  57. key: "setThumbnailViewer",
  58. value: function setThumbnailViewer(pdfThumbnailViewer) {
  59. this.pdfThumbnailViewer = pdfThumbnailViewer;
  60. }
  61. }, {
  62. key: "isHighestPriority",
  63. value: function isHighestPriority(view) {
  64. return this.highestPriorityPage === view.renderingId;
  65. }
  66. }, {
  67. key: "renderHighestPriority",
  68. value: function renderHighestPriority(currentlyVisiblePages) {
  69. if (this.idleTimeout) {
  70. clearTimeout(this.idleTimeout);
  71. this.idleTimeout = null;
  72. }
  73. if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
  74. return;
  75. }
  76. if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
  77. if (this.pdfThumbnailViewer.forceRendering()) {
  78. return;
  79. }
  80. }
  81. if (this.printing) {
  82. return;
  83. }
  84. if (this.onIdle) {
  85. this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
  86. }
  87. }
  88. }, {
  89. key: "getHighestPriority",
  90. value: function getHighestPriority(visible, views, scrolledDown) {
  91. var visibleViews = visible.views;
  92. var numVisible = visibleViews.length;
  93. if (numVisible === 0) {
  94. return false;
  95. }
  96. for (var i = 0; i < numVisible; ++i) {
  97. var view = visibleViews[i].view;
  98. if (!this.isViewFinished(view)) {
  99. return view;
  100. }
  101. }
  102. if (scrolledDown) {
  103. var nextPageIndex = visible.last.id;
  104. if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex])) {
  105. return views[nextPageIndex];
  106. }
  107. } else {
  108. var previousPageIndex = visible.first.id - 2;
  109. if (views[previousPageIndex] && !this.isViewFinished(views[previousPageIndex])) {
  110. return views[previousPageIndex];
  111. }
  112. }
  113. return null;
  114. }
  115. }, {
  116. key: "isViewFinished",
  117. value: function isViewFinished(view) {
  118. return view.renderingState === RenderingStates.FINISHED;
  119. }
  120. }, {
  121. key: "renderView",
  122. value: function renderView(view) {
  123. var _this = this;
  124. switch (view.renderingState) {
  125. case RenderingStates.FINISHED:
  126. return false;
  127. case RenderingStates.PAUSED:
  128. this.highestPriorityPage = view.renderingId;
  129. view.resume();
  130. break;
  131. case RenderingStates.RUNNING:
  132. this.highestPriorityPage = view.renderingId;
  133. break;
  134. case RenderingStates.INITIAL:
  135. this.highestPriorityPage = view.renderingId;
  136. var continueRendering = function continueRendering() {
  137. _this.renderHighestPriority();
  138. };
  139. view.draw().then(continueRendering, continueRendering);
  140. break;
  141. }
  142. return true;
  143. }
  144. }]);
  145. return PDFRenderingQueue;
  146. }();
  147. exports.PDFRenderingQueue = PDFRenderingQueue;