]> git.parisson.com Git - pdf.js.git/commitdiff
Add a cache system to limit the amount of ram used
authorVivien Nicolas <21@vingtetun.org>
Fri, 29 Jul 2011 11:26:43 +0000 (13:26 +0200)
committerVivien Nicolas <21@vingtetun.org>
Fri, 29 Jul 2011 11:26:43 +0000 (13:26 +0200)
web/viewer.js

index 8f245028e06487224e5c82c3968a5f8a5c393f20..3283883967d9418aa81f3f5c7ba3f95a980b5637 100644 (file)
@@ -6,6 +6,19 @@
 var kDefaultURL = 'compressed.tracemonkey-pldi-09.pdf';
 var kDefaultScale = 150;
 
+var kCacheSize = 20;
+
+var Cache = function(size) {
+  var data = [];
+  this.push = function(view) {
+    data.push(view);
+    if (data.length > size)
+      data.shift().update();
+  };
+};
+
+var cache = new Cache(kCacheSize);
+
 var PDFView = {
   pages: [],
   thumbnails: [],
@@ -121,7 +134,7 @@ var PDFView = {
     var windowBottom = window.pageYOffset + window.innerHeight;
     for (; i <= pages.length && currentHeight < windowBottom; i++) {
       var page = pages[i - 1];
-      visiblePages.push({ id: page.id, y: currentHeight });
+      visiblePages.push({ id: page.id, y: currentHeight, view: page });
       currentHeight += page.height * page.scale + kBottomMargin;
     }
 
@@ -146,7 +159,7 @@ var PageView = function(container, content, id, width, height, stats) {
   container.appendChild(div);
 
   this.update = function(scale) {
-    this.scale = scale;
+    this.scale = scale || this.scale;
     div.style.width =  (this.width * this.scale)+ 'px';
     div.style.height = (this.height * this.scale) + 'px';
 
@@ -157,7 +170,7 @@ var PageView = function(container, content, id, width, height, stats) {
   this.draw = function() {
     if (div.hasChildNodes()) {
       this.updateStats();
-      return;
+      return false;
     }
 
     var canvas = document.createElement('canvas');
@@ -176,6 +189,8 @@ var PageView = function(container, content, id, width, height, stats) {
 
     stats.begin = Date.now();
     this.content.startRendering(ctx, this.updateStats);
+
+    return true;
   };
 
   this.updateStats = function() {
@@ -240,8 +255,11 @@ window.addEventListener('pdfloaded', function(evt) {
 
 window.addEventListener('scroll', function(evt) {
   var visiblePages = PDFView.getVisiblePages();
-  for (var i = 0; i < visiblePages.length; i++)
-    PDFView.pages[visiblePages[i].id - 1].draw();
+  for (var i = 0; i < visiblePages.length; i++) {
+    var page = visiblePages[i];
+    if (PDFView.pages[page.id - 1].draw())
+      cache.push(page.view);
+  }
 
   if (!visiblePages.length)
     return;