pdf_rendering_queue.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. renderHighestPriority(currentlyVisiblePages) {
  56. if (this.idleTimeout) {
  57. clearTimeout(this.idleTimeout);
  58. this.idleTimeout = null;
  59. }
  60. if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
  61. return;
  62. }
  63. if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
  64. if (this.pdfThumbnailViewer.forceRendering()) {
  65. return;
  66. }
  67. }
  68. if (this.printing) {
  69. return;
  70. }
  71. if (this.onIdle) {
  72. this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
  73. }
  74. }
  75. getHighestPriority(visible, views, scrolledDown) {
  76. const visibleViews = visible.views;
  77. const numVisible = visibleViews.length;
  78. if (numVisible === 0) {
  79. return null;
  80. }
  81. for (let i = 0; i < numVisible; ++i) {
  82. const view = visibleViews[i].view;
  83. if (!this.isViewFinished(view)) {
  84. return view;
  85. }
  86. }
  87. if (scrolledDown) {
  88. const nextPageIndex = visible.last.id;
  89. if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex])) {
  90. return views[nextPageIndex];
  91. }
  92. } else {
  93. const previousPageIndex = visible.first.id - 2;
  94. if (views[previousPageIndex] && !this.isViewFinished(views[previousPageIndex])) {
  95. return views[previousPageIndex];
  96. }
  97. }
  98. return null;
  99. }
  100. isViewFinished(view) {
  101. return view.renderingState === RenderingStates.FINISHED;
  102. }
  103. renderView(view) {
  104. switch (view.renderingState) {
  105. case RenderingStates.FINISHED:
  106. return false;
  107. case RenderingStates.PAUSED:
  108. this.highestPriorityPage = view.renderingId;
  109. view.resume();
  110. break;
  111. case RenderingStates.RUNNING:
  112. this.highestPriorityPage = view.renderingId;
  113. break;
  114. case RenderingStates.INITIAL:
  115. this.highestPriorityPage = view.renderingId;
  116. view.draw().finally(() => {
  117. this.renderHighestPriority();
  118. }).catch(reason => {
  119. if (reason instanceof _pdf.RenderingCancelledException) {
  120. return;
  121. }
  122. console.error(`renderView: "${reason}"`);
  123. });
  124. break;
  125. }
  126. return true;
  127. }
  128. }
  129. exports.PDFRenderingQueue = PDFRenderingQueue;