text: /* Copyright 2017 Mozilla Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  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; }; }();
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  
  var CLEANUP_TIMEOUT = 30000;
  var RenderingStates = {
    INITIAL: 0,
    RUNNING: 1,
    PAUSED: 2,
    FINISHED: 3
  };
  
  var PDFRenderingQueue = function () {
    function PDFRenderingQueue() {
      _classCallCheck(this, PDFRenderingQueue);
  
      this.pdfViewer = null;
      this.pdfThumbnailViewer = null;
      this.onIdle = null;
      this.highestPriorityPage = null;
      this.idleTimeout = null;
      this.printing = false;
      this.isThumbnailViewEnabled = false;
    }
  
    _createClass(PDFRenderingQueue, [{
      key: "setViewer",
      value: function setViewer(pdfViewer) {
        this.pdfViewer = pdfViewer;
      }
    }, {
      key: "setThumbnailViewer",
      value: function setThumbnailViewer(pdfThumbnailViewer) {
        this.pdfThumbnailViewer = pdfThumbnailViewer;
      }
    }, {
      key: "isHighestPriority",
      value: function isHighestPriority(view) {
        return this.highestPriorityPage === view.renderingId;
      }
    }, {
      key: "renderHighestPriority",
      value: function renderHighestPriority(currentlyVisiblePages) {
        if (this.idleTimeout) {
          clearTimeout(this.idleTimeout);
          this.idleTimeout = null;
        }
        if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
          return;
        }
        if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
          if (this.pdfThumbnailViewer.forceRendering()) {
            return;
          }
        }
        if (this.printing) {
          return;
        }
        if (this.onIdle) {
          this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
        }
      }
    }, {
      key: "getHighestPriority",
      value: function getHighestPriority(visible, views, scrolledDown) {
        var visibleViews = visible.views;
        var numVisible = visibleViews.length;
        if (numVisible === 0) {
          return false;
        }
        for (var i = 0; i < numVisible; ++i) {
          var view = visibleViews[i].view;
          if (!this.isViewFinished(view)) {
            return view;
          }
        }
        if (scrolledDown) {
          var nextPageIndex = visible.last.id;
          if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex])) {
            return views[nextPageIndex];
          }
        } else {
          var previousPageIndex = visible.first.id - 2;
          if (views[previousPageIndex] && !this.isViewFinished(views[previousPageIndex])) {
            return views[previousPageIndex];
          }
        }
        return null;
      }
    }, {
      key: "isViewFinished",
      value: function isViewFinished(view) {
        return view.renderingState === RenderingStates.FINISHED;
      }
    }, {
      key: "renderView",
      value: function renderView(view) {
        var _this = this;
  
        switch (view.renderingState) {
          case RenderingStates.FINISHED:
            return false;
          case RenderingStates.PAUSED:
            this.highestPriorityPage = view.renderingId;
            view.resume();
            break;
          case RenderingStates.RUNNING:
            this.highestPriorityPage = view.renderingId;
            break;
          case RenderingStates.INITIAL:
            this.highestPriorityPage = view.renderingId;
            var continueRendering = function continueRendering() {
              _this.renderHighestPriority();
            };
            view.draw().then(continueRendering, continueRendering);
            break;
        }
        return true;
      }
    }]);
  
    return PDFRenderingQueue;
  }();
  
  exports.RenderingStates = RenderingStates;
  exports.PDFRenderingQueue = PDFRenderingQueue;