pdf_rendering_queue.js 3.6 KB

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