pdf_rendering_queue.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.RenderingStates = exports.PDFRenderingQueue = void 0;
  27. var _pdf = require("../pdf");
  28. const CLEANUP_TIMEOUT = 30000;
  29. const RenderingStates = {
  30. INITIAL: 0,
  31. RUNNING: 1,
  32. PAUSED: 2,
  33. FINISHED: 3
  34. };
  35. exports.RenderingStates = RenderingStates;
  36. class PDFRenderingQueue {
  37. constructor() {
  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. setViewer(pdfViewer) {
  47. this.pdfViewer = pdfViewer;
  48. }
  49. setThumbnailViewer(pdfThumbnailViewer) {
  50. this.pdfThumbnailViewer = pdfThumbnailViewer;
  51. }
  52. isHighestPriority(view) {
  53. return this.highestPriorityPage === view.renderingId;
  54. }
  55. hasViewer() {
  56. return !!this.pdfViewer;
  57. }
  58. renderHighestPriority(currentlyVisiblePages) {
  59. if (this.idleTimeout) {
  60. clearTimeout(this.idleTimeout);
  61. this.idleTimeout = null;
  62. }
  63. if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
  64. return;
  65. }
  66. if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
  67. if (this.pdfThumbnailViewer.forceRendering()) {
  68. return;
  69. }
  70. }
  71. if (this.printing) {
  72. return;
  73. }
  74. if (this.onIdle) {
  75. this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
  76. }
  77. }
  78. getHighestPriority(visible, views, scrolledDown, preRenderExtra = false) {
  79. const visibleViews = visible.views;
  80. const numVisible = visibleViews.length;
  81. if (numVisible === 0) {
  82. return null;
  83. }
  84. for (let i = 0; i < numVisible; ++i) {
  85. const view = visibleViews[i].view;
  86. if (!this.isViewFinished(view)) {
  87. return view;
  88. }
  89. }
  90. let preRenderIndex = scrolledDown ? visible.last.id : visible.first.id - 2;
  91. let preRenderView = views[preRenderIndex];
  92. if (preRenderView && !this.isViewFinished(preRenderView)) {
  93. return preRenderView;
  94. }
  95. if (preRenderExtra) {
  96. preRenderIndex += scrolledDown ? 1 : -1;
  97. preRenderView = views[preRenderIndex];
  98. if (preRenderView && !this.isViewFinished(preRenderView)) {
  99. return preRenderView;
  100. }
  101. }
  102. return null;
  103. }
  104. isViewFinished(view) {
  105. return view.renderingState === RenderingStates.FINISHED;
  106. }
  107. renderView(view) {
  108. switch (view.renderingState) {
  109. case RenderingStates.FINISHED:
  110. return false;
  111. case RenderingStates.PAUSED:
  112. this.highestPriorityPage = view.renderingId;
  113. view.resume();
  114. break;
  115. case RenderingStates.RUNNING:
  116. this.highestPriorityPage = view.renderingId;
  117. break;
  118. case RenderingStates.INITIAL:
  119. this.highestPriorityPage = view.renderingId;
  120. view.draw().finally(() => {
  121. this.renderHighestPriority();
  122. }).catch(reason => {
  123. if (reason instanceof _pdf.RenderingCancelledException) {
  124. return;
  125. }
  126. console.error(`renderView: "${reason}"`);
  127. });
  128. break;
  129. }
  130. return true;
  131. }
  132. }
  133. exports.PDFRenderingQueue = PDFRenderingQueue;