Skip to content

Commit

Permalink
resourcecache to imagecache
Browse files Browse the repository at this point in the history
  • Loading branch information
hallysonh committed Dec 29, 2017
1 parent 9d9c3af commit d990d53
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hallysonh/ngx-imageviewer",
"version": "1.1.1",
"version": "1.1.2",
"license": "MIT",
"author": {
"name": "Hallyson Almeida",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Injectable } from '@angular/core';
export interface CacheDef {
url: string;
page: number;
resource: string;
image: any;
}

@Injectable()
export class ResourceCacheService {
export class ImageCacheService {

private _cache: CacheDef[] = [];

Expand All @@ -21,22 +21,22 @@ export class ResourceCacheService {
return this.cache.find(i => i.url === url && i.page === page);
}

getResource(url: string, page: number) {
getImage(url: string, page: number) {
const c = this.getCache(url, page);
return c ? c.resource : null;
return c ? c.image : null;
}

saveResource(url: string, page: number, resource: string) {
saveImage(url: string, page: number, image: any) {
const cache = this.getCache(url, page);
if (cache) {
cache.resource = resource;
cache.image = image;
} else {
this.cache.push({ url, page, resource });
this.cache.push({ url, page, image });
}
}

disposeCache() {
this.cache.forEach(i => URL.revokeObjectURL(i.resource));
this.cache.forEach(i => URL.revokeObjectURL(i.image.src));
this._cache = [];
}
}
8 changes: 4 additions & 4 deletions src/app/imageviewer/imageviewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Viewport, Button, toSquareAngle, ResourceLoader } from './imageviewer.m
import { Subscription } from 'rxjs/Subscription';
import { ImageResourceLoader } from './image.loader';
import { PdfResourceLoader } from './pdf.loader';
import { ResourceCacheService } from './resourcecache.service';
import { ImageCacheService } from './imagecache.service';

const MIN_TOOLTIP_WIDTH_SPACE = 500;

Expand Down Expand Up @@ -110,7 +110,7 @@ export class ImageViewerComponent implements AfterViewInit, OnDestroy {
constructor(
private _sanitizer: DomSanitizer,
private _renderer: Renderer,
private _resourceCache: ResourceCacheService,
private _imageCache: ImageCacheService,
@Inject(IMAGEVIEWER_CONFIG) private config: ImageViewerConfig
) {
this.config = this.extendsDefaultConfig(config);
Expand Down Expand Up @@ -161,7 +161,7 @@ export class ImageViewerComponent implements AfterViewInit, OnDestroy {
listenDestroy();
}
});
this._resourceCache.disposeCache();
this._imageCache.disposeCache();
}

setUpResource() {
Expand All @@ -178,7 +178,7 @@ export class ImageViewerComponent implements AfterViewInit, OnDestroy {
this._resourceChangeSub.unsubscribe();
}
if (!this._pdfResource) {
this._pdfResource = new PdfResourceLoader(this._resourceCache);
this._pdfResource = new PdfResourceLoader(this._imageCache);
}
this._resource = this._pdfResource;
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/imageviewer/imageviewer.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NgModule, InjectionToken } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ResourceCacheService } from './resourcecache.service';
import { ImageCacheService } from './imagecache.service';
import { ImageViewerComponent } from './imageviewer.component';
import { ImageViewerConfig, IMAGEVIEWER_CONFIG, IMAGEVIEWER_CONFIG_DEFAULT } from './imageviewer.config';

Expand All @@ -13,7 +13,7 @@ import { ImageViewerConfig, IMAGEVIEWER_CONFIG, IMAGEVIEWER_CONFIG_DEFAULT } fro
providers: [{
provide: IMAGEVIEWER_CONFIG,
useValue: IMAGEVIEWER_CONFIG_DEFAULT
}, ResourceCacheService],
}, ImageCacheService],
exports: [ImageViewerComponent]
})
export class ImageViewerModule { }
16 changes: 7 additions & 9 deletions src/app/imageviewer/pdf.loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResourceCacheService } from './resourcecache.service';
import { ImageCacheService } from './imagecache.service';
import { ResourceLoader, Dimension, toSquareAngle } from './imageviewer.model';
import { ImageViewerConfig } from './imageviewer.config';

Expand All @@ -9,7 +9,7 @@ export class PdfResourceLoader extends ResourceLoader {
private _page;
private _pendingReload;

constructor(private _resourceCache: ResourceCacheService) {
constructor(private _imageCache: ImageCacheService) {
super();
if (!PDFJS.workerSrc) {
PDFJS.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${(PDFJS as any).version}/pdf.worker.min.js`;
Expand Down Expand Up @@ -60,12 +60,10 @@ export class PdfResourceLoader extends ResourceLoader {

private loadImage(src: string, page: number, onFinish: () => void) {
const vm = this;

if (this._resourceCache.getResource(src, page)) {
const img = new Image();
img.onload = onFinish;
img.src = vm._resourceCache.getResource(vm.src, vm.currentItem);
vm._image = img;
const cacheimg = this._imageCache.getImage(src, page);
if (cacheimg) {
vm._image = cacheimg;
onFinish();
return;
}

Expand All @@ -86,7 +84,7 @@ export class PdfResourceLoader extends ResourceLoader {
const img = new Image();
img.onload = onFinish;
img.src = URL.createObjectURL(blob);
vm._resourceCache.saveResource(src, page, img.src);
vm._imageCache.saveImage(src, page, img);
vm._image = img;
});
});
Expand Down

0 comments on commit d990d53

Please sign in to comment.