pdf_rendering_queue.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  27. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  28. var CLEANUP_TIMEOUT = 30000;
  29. var RenderingStates = {
  30. INITIAL: 0,
  31. RUNNING: 1,
  32. PAUSED: 2,
  33. FINISHED: 3
  34. };
  35. var PDFRenderingQueue = function () {
  36. function PDFRenderingQueue() {
  37. _classCallCheck(this, PDFRenderingQueue);
  38. this.pdfViewer = null;
  39. this.pdfThumbnailViewer = null;
  40. this.onIdle = null;
  41. this.highestPriorityPage = null;
  42. this.idleTimeout = null;
  43. this.printing = false;
  44. this.isThumbnailViewEnabled = false;
  45. }
  46. _createClass(PDFRenderingQueue, [{
  47. key: "setViewer",
  48. value: function setViewer(pdfViewer) {
  49. this.pdfViewer = pdfViewer;
  50. }
  51. }, {
  52. key: "setThumbnailViewer",
  53. value: function setThumbnailViewer(pdfThumbnailViewer) {
  54. this.pdfThumbnailViewer = pdfThumbnailViewer;
  55. }
  56. }, {
  57. key: "isHighestPriority",
  58. value: function isHighestPriority(view) {
  59. return this.highestPriorityPage === view.renderingId;
  60. }
  61. }, {
  62. key: "renderHighestPriority",
  63. value: function renderHighestPriority(currentlyVisiblePages) {
  64. if (this.idleTimeout) {
  65. clearTimeout(this.idleTimeout);
  66. this.idleTimeout = null;
  67. }
  68. if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
  69. return;
  70. }
  71. if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
  72. if (this.pdfThumbnailViewer.forceRendering()) {
  73. return;
  74. }
  75. }
  76. if (this.printing) {
  77. return;
  78. }
  79. if (this.onIdle) {
  80. this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
  81. }
  82. }
  83. }, {
  84. key: "getHighestPriority",
  85. value: function getHighestPriority(visible, views, scrolledDown) {
  86. var visibleViews = visible.views;
  87. var numVisible = visibleViews.length;
  88. if (numVisible === 0) {
  89. return false;
  90. }
  91. for (var i = 0; i < numVisible; ++i) {
  92. var view = visibleViews[i].view;
  93. if (!this.isViewFinished(view)) {
  94. return view;
  95. }
  96. }
  97. if (scrolledDown) {
  98. var nextPageIndex = visible.last.id;
  99. if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex])) {
  100. return views[nextPageIndex];
  101. }
  102. } else {
  103. var previousPageIndex = visible.first.id - 2;
  104. if (views[previousPageIndex] && !this.isViewFinished(views[previousPageIndex])) {
  105. return views[previousPageIndex];
  106. }
  107. }
  108. return null;
  109. }
  110. }, {
  111. key: "isViewFinished",
  112. value: function isViewFinished(view) {
  113. return view.renderingState === RenderingStates.FINISHED;
  114. }
  115. }, {
  116. key: "renderView",
  117. value: function renderView(view) {
  118. var _this = this;
  119. switch (view.renderingState) {
  120. case RenderingStates.FINISHED:
  121. return false;
  122. case RenderingStates.PAUSED:
  123. this.highestPriorityPage = view.renderingId;
  124. view.resume();
  125. break;
  126. case RenderingStates.RUNNING:
  127. this.highestPriorityPage = view.renderingId;
  128. break;
  129. case RenderingStates.INITIAL:
  130. this.highestPriorityPage = view.renderingId;
  131. var continueRendering = function continueRendering() {
  132. _this.renderHighestPriority();
  133. };
  134. view.draw().then(continueRendering, continueRendering);
  135. break;
  136. }
  137. return true;
  138. }
  139. }]);
  140. return PDFRenderingQueue;
  141. }();
  142. exports.RenderingStates = RenderingStates;
  143. exports.PDFRenderingQueue = PDFRenderingQueue;